繁体   English   中英

根据boolean列过滤pandas dataframe行

[英]Filtering pandas dataframe rows based on boolean columns

我的 pandas dataframe 看起来像这样:

             col_1  | col_2  | col_3 .... col_100
date
01-01-2001   True    False  False   ...   True
02-01-2001   False   True   False   ...   True
03-01-2001   True    False  True    ...   True
04-01-2001   False   False  False   ...   False

结果,我想得到一个 df,其中包含行中至少有一个 True 的所有行。 在这种情况下,结果将是

             col_1  | col_2  | col_3 ... col_100
date
01-01-2001   True    False  False   ...  True
02-01-2001   False   True   False   ...  True
03-01-2001   True    False  True    ...  False

有什么聪明的方法可以做到这一点?

使用DataFrame.any

df1 = df[df.any(axis=1)]

开箱即用:

df1 = df[df.sum(axis=1).gt(0)]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM