groupby.count in pandas有条件

发布于 2025-01-19 12:19:00 字数 373 浏览 2 评论 0原文

我有13年的天气数据,每年有154天(仅对夏季感兴趣),我想从中获得每年的干燥天数。这意味着我有一个很棒的数据(DF)集,我想收缩(DF_CH)。我已经完成了每年的总降雨:

df_Ch = df.groupby(by = ["distrito", "year"], as_index=False)["Chuvia"].sum()    

我现在每年都在尝试总干日:

df_Ds = df.groupby(by = ["distrito", "year"], as_index=False)[when "Chuvia" == 0].count()

当然,它无效 我花了几个小时而没有成功地尝试得到一些建议?

I have weather data from 13 years, with 154 days each year (only interested in summer days), from which I would like to get the number of dry days occurred each year. This means I have a great data (df) set that I want to shrink (df_Ch). I've already done with the total rain per year:

df_Ch = df.groupby(by = ["distrito", "year"], as_index=False)["Chuvia"].sum()    

I'm now trying with total dry days per year:

df_Ds = df.groupby(by = ["distrito", "year"], as_index=False)[when "Chuvia" == 0].count()

Of course, it didn't work
I spent some hours without success trying to get something... Any suggestion?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

萌辣 2025-01-26 12:19:00

在使用groupby之前应用条件,这样您在分组之前就已经有了“过滤后的”df。

df_Ds = df[df["Chuvia" == 0]].groupby(by = ["distrito", "year"], as_index=False)["Chuvia"].count()

Apply the condition before using groupby, so you will already have your "filtered" df before grouping it.

df_Ds = df[df["Chuvia" == 0]].groupby(by = ["distrito", "year"], as_index=False)["Chuvia"].count()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文