NBC*_*NBC 6 python string performance pandas
我目前正在遵循此处列出的用于查找值的说明,并且它有效.唯一的问题是我的数据帧非常大(5x3500行),我需要执行约2000次搜索.每一个大约需要4秒钟,所以显然这会加起来并且在我的结尾变得有点不可持续.
最简洁的方法来选择任何列包含Pandas数据帧中的字符串的行?
有没有更快的方法来搜索包含字符串值的所有行?
df[df.apply(lambda r: r.str.contains('b', case=False).any(), axis=1)]
Run Code Online (Sandbox Code Playgroud)
你可以测试一下速度
boolfilter=(np.char.find(df.values.ravel().astype(str),'b')!=-1).reshape(df.shape).any(1)
boolfilter
array([False, True, True])
newdf=df[boolfilter]
Run Code Online (Sandbox Code Playgroud)