Python 删除一列熊猫数据框中包含“假”的行

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

Remove rows that contain 'False' in a column of pandas data frame

pythonpandasdataframeboolean

提问by Yolo_chicken

I assume this is an easy fix and I'm not sure what I'm missing. I have a data frame as such:

我认为这是一个简单的解决方法,我不确定我错过了什么。我有一个这样的数据框:

         index               c1       c2         c3
2015-03-07 01:27:05        False    False       True   
2015-03-07 01:27:10        False    False       True   
2015-03-07 01:27:15        False    False       False   
2015-03-07 01:27:20        False    False       True   
2015-03-07 01:27:25        False    False       False   
2015-03-07 01:27:30        False    False       True   

I want to remove any rows that contain Falsein c3. c3is a dtype=bool. I'm consistently running into problems since it's a boolean and not a string/int/etc, I haven't handled that before.

我想删除包含Falsec3. c3是一个dtype=bool。我一直遇到问题,因为它是布尔值而不是字符串/整数/等,我以前没有处理过。

Thanks for any help!

谢谢你的帮助!

回答by ASGM

Pandas deals with booleans in a really neat, straightforward manner:

Pandas 以一种非常简洁、直接的方式处理布尔值:

df = df[df.c3]

This does the same thing but without creating a copy (making it faster):

这做同样的事情,但不创建副本(使其更快):

df = df.loc[df.c3, :]

When you're filtering dataframes using df[...], you often write some function that returns a boolean value (like df.x > 2). But in this case, since the column is already a boolean, you can just put df.c3in on its own, which will get you all the rows that are True.

当您使用 过滤数据帧时df[...],您通常会编写一些返回布尔值的函数(如df.x > 2)。但在这种情况下,由于该列已经是一个布尔值,您可以单独df.c3输入,这将使您获得所有True.

If you wanted to get the opposite (as the original title to your question implied), you could use df[~df.c3]or df.loc[~df.c3, :], where the ~inverts the booleans.

如果您想得到相反的结果(正如您问题的原始标题所暗示的那样),您可以使用df[~df.c3]or df.loc[~df.c3, :],其中~反转布尔值。

For more on boolean indexing in Pandas, see the docs. Thanks to @Mr_and_Mrs_D for the suggestion about .loc.

有关 Pandas 中布尔索引的更多信息,请参阅文档。感谢@Mr_and_Mrs_D 的建议.loc

回答by DeepSpace

Well the question's title and the question itself are the exact opposite, but:

那么问题的标题和问题本身是完全相反的,但是:

df = df[df['c3'] == True]  # df will have only rows with True in c3

回答by piRSquared

Solution

解决方案

df.drop(df[df['c3'] == False].index, inplace=True)

This explicitly drops rows where 'c3'is Falseand not just keeping rows that evaluate to True

这明确地滴行,其中'c3'False不只是保持行评估为True