简体   繁体   English

根据boolean列过滤pandas dataframe行

[英]Filtering pandas dataframe rows based on boolean columns

My pandas dataframe looks like this:我的 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

as a result, I'd like to get a df that contains all the rows which have at least one True in the row.结果,我想得到一个 df,其中包含行中至少有一个 True 的所有行。 In this case, the results would be在这种情况下,结果将是

             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

Any clever way to do this?有什么聪明的方法可以做到这一点?

Use DataFrame.any :使用DataFrame.any

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

Out of box:开箱即用:

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

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

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