Pandas:类似函数的 grep

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12625650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 20:26:45  来源:igfitidea点击:

Pandas: grep like function

greprowpandas

提问by fred

Is there a grep like built-in function in Pandas to drop a row if it has some string or value? Thanks in advance.

Pandas 中是否有类似 grep 的内置函数来删除具有某些字符串或值的行?提前致谢。

回答by Wouter Overmeire

Have a look at df['column_label].str Below example will drop all rows where column A holds 'a' character and 'B' equals 20.

看看 df['column_label].str 下面的示例将删除列 A 包含 'a' 字符且 'B' 等于 20 的所有行。

In [46]: df
Out[46]:
     A   B
0  foo  10
1  bar  20
2  baz  30

In [47]: cond = df['A'].str.contains('a') & (df['B'] == 20)

In [48]: df.drop(df[cond].index.values)
Out[48]:
     A   B
0  foo  10
2  baz  30