Pandas Groupby 应用函数计算大于零的值 [英] Pandas Groupby apply function to count values greater than zero
问题描述
Pandas Groupby 应用函数计算大于零的值
Pandas Groupby apply function to count values greater than zero
我以下列方式使用 groupby 和 agg:
I am using groupby and agg in the following manner:
df.groupby('group')['a'].agg({'mean' : np.mean, 'std' : np.std})
并且我还想计算同一列 ['a'] 中高于零的值
and I would like to also count the values above zero in the same column ['a']
以下行按照我的意愿进行计数,
the following line does the count as I want,
sum(x > 0 for x in df['a'])
但是我在申请 groupby 时无法使用它.
but I can't get it work when applying to groupby.
以下示例将 Pandas 计算应用于我尝试过的 groupby:
Following an example for applying a pandas calculation to a groupby I tried:
df.groupby('group')['a'].apply(sum(x > 0 for x in df['a']))
但我收到一条错误消息:AttributeError: 'numpy.int32' 对象没有属性 'module'
but I get an error message: AttributeError: 'numpy.int32' object has no attribute 'module'
有人可以建议如何做到这一点吗?
Can anybody please suggest how this might be done?
推荐答案
来自评论的回答:
.agg({'pos':lambda ts: (ts > 0).sum()}) # – behzad.nouri Mar 31 at 0:00
这是我对未回答问题积压的贡献:)归功于 behzad.nouri
This is my contribution to the backlog of unanswered questions :) Credits to behzad.nouri
2020 年更新在最新的pandas版本中,需要做如下操作:
Update 2020 In the latest pandas version, you need to do the following:
.agg(pos=lambda ts: (ts > 0).sum())
否则会导致如下错误:
SpecificationError: nested renamer is not supported
这篇关于Pandas Groupby 应用函数计算大于零的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!