pandas 如果列值从数据框中删除行

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

Remove row from dataframe if column value

pythonpandasindexingdataframefilter

提问by vandelay

If I have a dataframe like this:

如果我有这样的数据框:

index   col1   col2 
a        a1     a2
b        b1     b2
c        c1     b2

Is it possible to use the .drop()to delete rows which have a sudden value in one of their columns?

是否可以使用.drop()删除在其中一列中具有突然值的行?

Something like: df.drop(col2 = 'b2')

就像是: df.drop(col2 = 'b2')

回答by jezrael

You need boolean indexing:

你需要boolean indexing

df = df[df.col2 != 'b2']

回答by Sohier Dane

You can use a boolean filter. For example:

您可以使用布尔过滤器。例如:

df = df[df['col1'] != 'a1']

Would drop all rows with the value a1 in column col1. See this page of the Pandas docsfor more details.

将删除列 col1 中值为 a1 的所有行。有关更多详细信息,请参阅Pandas 文档的此页面