asd
asd

Reputation: 1309

Drop boolean rows from dataframe

How can I quickly drop rows where A, B and C are all false? I tried:

df3 = df[~(~(df['A'])& ~(df['B']) & ~(df['C']) )]
df3
   com     A        B           C
0  as     TRUE    FALSE       FALSE
1  da     TRUE    FALSE       FALSE

Upvotes: 3

Views: 344

Answers (3)

BENY
BENY

Reputation: 323226

Wait a mins any

df[df[['A','B','C']].any(1)]

Upvotes: 4

Chris Adams
Chris Adams

Reputation: 18647

An alternative using all along axis 1. The 2nd ~ operator directly - directly before df, reversed all False to True. all returns True if all column values are True, then using ~ again to reverse this to index rows that are actually all False:

df3 = df[~(~df[['A', 'B', 'C']]).all(1)]

Upvotes: 2

anky
anky

Reputation: 75080

drop rows where A, B and C are are all false

With df.sum across axis=1 along with comparison if sum in the row for these coumns is not equal to 0 , using df.ne

out = df[df[['A','B','C']].sum(1).ne(0)].copy()

Upvotes: 5

Related Questions