If it's desired to filter multiple rows with None values, we could use any
, all
or sum
. For example, for df
given below:
FACTS_Value Region City Village
0 16482 Al Bahah None None
1 22522 Al Bahah Al Aqiq None
2 12444 Al Bahah Al Aqiq Al Aqiq
3 12823 Al Bahah Al Bahah Al Aqiq
4 11874 None None None
If we want to select all rows with None value in at least one column, we could use isna
+ any
on axis to build a boolean mask:
msk = df.isna().any(axis=1)
out = df[msk]
Output:
FACTS_Value Region City Village
0 16482 Al Bahah None None
1 22522 Al Bahah Al Aqiq None
4 11874 None None None
If we want the rows where all non-numeric column values are None, then we could use isna
+ all
on axis:
msk = df.select_dtypes(exclude='number').isna().all(axis=1)
or
msk = df[['Region', 'City', 'Village']].isna().all(axis=1)
out = df[msk]
Output:
FACTS_Value Region City Village
4 11874 None None None
If we want to filter rows where there are exactly n
None values, then we could use sum
on axis + eq
:
msk = df.isna().sum(axis=1) == 2
out = df[msk]
Output:
FACTS_Value Region City Village
0 16482 Al Bahah None None