计算df.groupby python中的NAS数量

发布于 2025-01-25 11:51:22 字数 700 浏览 4 评论 0原文

我正在使用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:

screenshot

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不羁少年 2025-02-01 11:51:22

如果按“每个变量”为“每个数据帧”列的意思是这样做:

df['column name'].isna().sum()

如果要使用groupby,则可以在此处找到一个解决方案:
pandas count count null null null值

If by 'per variable' you mean per each dataframe column, you can do this:

df['column name'].isna().sum()

If you want to use groupby, you can find a solution here:
Pandas count null values in a groupby function

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文