[英]Pandas groupby column and sum nulls of all other columns
I have a dataframe with the following structure:我有一个具有以下结构的数据框:
import pandas as pd
df = pd.DataFrame({"a": [1, None, 2], "b": [4, 5, None], "group": ["a", "a", "b"]})
I'd like to know, grouping by group, how many nulls there are in each column.我想知道,按组分组,每列中有多少个空值。
In this case, the output should be:在这种情况下,输出应该是:
group x y
0 a 1 0
1 b 0 1
I don't have control on how many columns I have or their names.我无法控制我有多少列或它们的名称。 Thanks!
谢谢!
Convert column group
to index, test all another values for misisng values by DataFrame.isna
, and for count True
s aggregate sum
:将列
group
转换为索引,通过DataFrame.isna
测试所有其他值的错误值,以及 count True
的sum
:
df = df.set_index('group').isna().groupby('group').sum().reset_index()
print(df)
group a b
0 a 1 0
1 b 0 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.