简体   繁体   中英

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 (ie, 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!

use pandas.DataFrame.all :

df[mask_df.all(axis = 1)]

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