计算df.groupby python中的NAS数量
我正在使用Python的数据框架,该数据框有很多NAS。我想计算每个变量的NAS数量。我已经浏览了文档,并找到了count()
,除此之外,还为我提供了相反的内容:
df.groupby("var1").count()
我的问题是,我如何仅计算Groupby中的NAS数量? 我尝试了:
df.groupby("var1").isnull() or df.groupby("var1").isna()
或者
df.groupby("var1").apply(isnull)
这给了我错误。
我想做的是:将数据库每个变量(在这种情况下为公民身份)组分组,然后计算其每个级别的NAS数量。
我希望输出像屏幕截图一样,但是使用NAS的数量,而不是观察值数量减去NAS数量:
或换句话说,我正在寻找python中此R代码的解决方案:
dat%>%group_by(citizenship)%>%summarise_all(funs(sum(is.na(.)))
I'm working with a data frame in python that has a lot of NAs. I'd like to count the number of NAs per variable. I've looked through the documentation and found the count()
, except it, gives me the opposite of what I want:
df.groupby("var1").count()
My question is, how can I instead only count the number of NAs in groupby?
I tried:
df.groupby("var1").isnull() or df.groupby("var1").isna()
or
df.groupby("var1").apply(isnull)
but that gives me errors.
What I'd like to do is this: group the database per variable (citizenship in this case) and then count the number of NAs for every level of its factor.
I'd like the output to be like the screenshot but with numbers of NAs instead of numbers of observations minus number of NAs as output:
Or, in other words, I am looking for a solution of this R code in Python:
dat%>%group_by(citizenship)%>%summarise_all(funs(sum(is.na(.)))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果按“每个变量”为“每个数据帧”列的意思是这样做:
如果要使用groupby,则可以在此处找到一个解决方案:
pandas count count null null null值
If by 'per variable' you mean per each dataframe column, you can do this:
If you want to use groupby, you can find a solution here:
Pandas count null values in a groupby function