Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count rows with 1 or more NaNs in a Dataframe

I have the following:

print(df.isna().sum())

Which gives me:

city                 2
country              0
testid               0
house             1807
house_number       248
po_box            1845
zipcode            260
road               132
state                1
state_district    1817
suburb            1800
unit              1806

I want the total number of rows that have 1 or more NaN values from columns city, state, zip, and house

Thanks for any suggestions.

like image 568
a1234 Avatar asked Jan 31 '26 18:01

a1234


1 Answers

This is how I would use isna and sum:

cols = ['city', 'state', 'zip', 'house']
df[df[cols].isna().sum(axis=1) > 0]

Another option is calling dropna and checking the length.

u = df.dropna(subset=['city', 'state', 'zip', 'house'])
len(df) - len(u)
like image 183
cs95 Avatar answered Feb 02 '26 10:02

cs95