TiagoPedro
TiagoPedro

Reputation: 15

pandas - select rows where the boolean filtering of a subset of columns are true

I have a dataframe 'df' from which I want to select the subset where 3 specific columns are not null.

So far I have tried to apply bool filtering

mask_df = df[['Empty', 'Peak', 'Full']].notnull()

which gives me the following result

    Empty   Peak    Full
0   True    False   False
1   False   False   False
2   True    True    True
3   False   False   False
4   False   False   False
... ... ... ...
2775244 True    True    True
2775245 True    True    True
2775246 False   False   False
2775247 False   False   False
2775248 False   False   False

Now I want to select ONLY the rows where the mask for those 3 columns is True (i.e., rows where those 3 columns have null values). If I filter the original dataframe 'df' with this mask I get the original dataframe full of null values, except those where the mask_df is "True".

I probably can do this by applying a lambda function row-wise, but I would prefer to avoid that computation if there was a simpler way to do this.

Thanks in advance!

Upvotes: 0

Views: 1118

Answers (1)

Pablo C
Pablo C

Reputation: 4761

use pandas.DataFrame.all:

df[mask_df.all(axis = 1)]

Upvotes: 1

Related Questions