Count rows with 1 or more NaNs in a Dataframe

三世轮回 提交于 2020-01-13 17:09:14

问题


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.


回答1:


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)


来源:https://stackoverflow.com/questions/54205469/count-rows-with-1-or-more-nans-in-a-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!