Reputation: 97
Is there any way to combine the two statements
df.isnull().sum()
and
(df == 0).sum()
to get the following overview?
Demo:
df = pd.DataFrame({'a':[1,0,0,1,3], 'b':[0,NaN,1,NaN,1], 'c':[0,0,0,0,NaN]})
df
a b c
0 1 0.0 0.0
1 0 NaN 0.0
2 0 1.0 0.0
3 1 NaN 0.0
4 3 1.0 NaN
Expected result:
a 2
b 3
c 5
Probably very simple, but I can't find the solution... Thank's for your help
Upvotes: 2
Views: 45
Reputation: 394279
You mean just this:
In[27]:
(df==0).sum() + df.isnull().sum()
Out[27]:
a 2
b 3
c 5
dtype: int64
EDIT
Thanks to @coldpseed for the suggestion, you can also do the following:
In[28]:
df[df!=0].isnull().sum()
Out[28]:
a 2
b 3
c 5
dtype: int64
which is more succinct, I've always been more in favour of clarity but shorter code sometimes wins.
Upvotes: 5
Reputation: 323366
With fillna
df.fillna(0).eq(0).sum()
Out[8]:
a 2
b 3
c 5
dtype: int64
Upvotes: 5
Reputation: 43534
Another option:
>>> ((df == 0) | df.isnull()).sum()
a 2
b 3
c 5
dtype: int64
>>>
Also:
>>> (df.eq(0) | df.isnull()).sum()
a 2
b 3
c 5
dtype: int64
>>>
Upvotes: 2