简体   繁体   中英

Filtering pandas dataframe rows based on boolean columns

My pandas dataframe looks like this:

             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. 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 :

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

Out of box:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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