![](/img/trans.png)
[英]Fastest way to remove rows that contain substrings of values in the same column of a pandas dataframe
[英]What is the fastest way to select rows that contain a value in a Pandas dataframe?
我目前正在遵循此处列出的用于查找值的说明,并且它有效。 唯一的问题是我的数据帧非常大(5x3500行),我需要执行约2000次搜索。 每一个大约需要4秒钟,所以显然这会加起来并且在我的结尾变得有点不可持续。
最简洁的方法来选择任何列包含Pandas数据帧中的字符串的行?
有没有更快的方法来搜索包含字符串值的所有行?
df[df.apply(lambda r: r.str.contains('b', case=False).any(), axis=1)]
你可以测试速度
boolfilter=(np.char.find(df.values.ravel().astype(str),'b')!=-1).reshape(df.shape).any(1)
boolfilter
array([False, True, True])
newdf=df[boolfilter]
一个微不足道的可能性是禁用正则表达式:
res = df[df.apply(lambda r: r.str.contains('b', case=False, regex=False).any(), axis=1)]
使用列表理解的另一种方法:
res = df[[any('b' in x.lower() for x in row) for row in df.values)]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.