I have a dataframe with the following structure:
JavaScript
x
4
1
import pandas as pd
2
3
df = pd.DataFrame({"a": [1, None, 2], "b": [4, 5, None], "group": ["a", "a", "b"]})
4
I’d like to know, grouping by group, how many nulls there are in each column.
In this case, the output should be:
JavaScript
1
4
1
group x y
2
0 a 1 0
3
1 b 0 1
4
I don’t have control on how many columns I have or their names. Thanks!
Advertisement
Answer
Convert column group
to index, test all another values for misisng values by DataFrame.isna
, and for count True
s aggregate sum
:
JavaScript
1
7
1
df = df.set_index('group').isna().groupby('group').sum().reset_index()
2
3
print(df)
4
group a b
5
0 a 1 0
6
1 b 0 1
7