gpt4 book ai didi

Python:在列表的 Pandas 列上进行字符串匹配

转载 作者:行者123 更新时间:2023-11-28 21:38:59 25 4
gpt4 key购买 nike

对列表的一列进行字符串匹配的最佳方法是什么?
例如。我有一个数据集:

import numpy as np
import pandas as pd
list_items = ['apple', 'grapple', 'tackle', 'satchel', 'snapple']
df = pd.DataFrame({'id':xrange(3), 'L':[np.random.choice(list_items, 3).tolist() for _ in xrange(3)]})
df

L id
0 [tackle, apple, grapple] 0
1 [tackle, snapple, satchel] 1
2 [satchel, satchel, tackle] 2

我想返回 L 中的任何项目与字符串匹配的行,例如“grap”应返回第 0 行,“sat”应返回第 1:2 行。

最佳答案

让我们用这个:

np.random.seed(123)
list_items = ['apple', 'grapple', 'tackle', 'satchel', 'snapple']
df = pd.DataFrame({'id':range(3), 'L':[np.random.choice(list_items, 3).tolist() for _ in range(3)]})
df
L id
0 [tackle, snapple, tackle] 0
1 [grapple, satchel, tackle] 1
2 [satchel, grapple, grapple] 2

使用anyapply:

df[df.L.apply(lambda x: any('grap' in s for s in x))]

输出:

                             L  id
1 [grapple, satchel, tackle] 1
2 [satchel, grapple, grapple] 2

时间:

%timeit df.L.apply(lambda x: any('grap' in s for s in x))

10000 loops, best of 3: 194 µs per loop

%timeit df.L.apply(lambda i: ','.join(i)).str.contains('grap')

1000 loops, best of 3: 481 µs per loop

%timeit df.L.str.join(', ').str.contains('grap')

1000 loops, best of 3: 529 µs per loop

关于Python:在列表的 Pandas 列上进行字符串匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47441980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com