Pandas:如果单元格包含特定文本,则删除行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51182182/
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-14 05:46:27  来源:igfitidea点击:

Pandas: Delete Row if cell contains specific text

pythonpandas

提问by 0004

this code in pandas does not work. I want it to delete the row if the column contains any of the text/numbers provided Currently I can only get it to work if the cell matches the exact text being passed in my code .. as in it only deletes cells that say Fin* not Finance or Finly...

Pandas中的这段代码不起作用。如果该列包含提供的任何文本/数字,我希望它删除该行目前我只能在单元格与我的代码中传递的确切文本匹配时才能使其工作..因为它只删除说 Fin* 的单元格不是财务或最终...

df2 = df[df.Team != 'Fin*']

回答by YOBEN_S

You can using startswith

你可以使用 startswith

df[~df.Team.str.startswith('Fin')]

Or

或者

df[~df.Team.str.contains('Fin')]

回答by min2bro

import pandas as pd
df = pd.DataFrame(dict(A=[1,2,3,4], C=["abc","def","abcdef", "lmn"]))

df:

    A   C
0   1   abc
1   2   def
2   3   abcdef
3   4   lmn

df[df.C.str.contains("abc") == False]

OR as suggested by @RafaelC

或者按照@RafaelC 的建议

df[~df.C.str.contains("abc")]

Output:

输出:

    A   C
1   2   def
3   4   lmn

回答by DYZ

You need regular expressions for this operation. Here's a synthetic dataframe:

此操作需要正则表达式。这是一个合成数据框:

df = pd.DataFrame({'Team': ['Finance', 'Finally', 'Foo']})

Here's a dataframe that does not (~) have any Fin's:

这是一个没有 ( ~)的数据框:

df[~df.Team.str.match('Fin*')]
#  Team
#2  Foo

If you are sure that a string of interest always starts with Fin, you can use a "softer" method:

如果您确定感兴趣的字符串始终以 Fin 开头,则可以使用“更软”的方法:

df[~df.Team.str.startswith('Fin')]
#  Team
#2  Foo