Home > Mobile >  Filtering pandas dataframe rows based on boolean columns
Filtering pandas dataframe rows based on boolean columns

Time:10-05

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?

CodePudding user response:

Use DataFrame.any:

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

Out of box:

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