Pandas Groupby 应用函数计算大于零的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22751498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas Groupby apply function to count values greater than zero
提问by rdh9
Pandas Groupby apply function to count values greater than zero
Pandas Groupby 应用函数计算大于零的值
I am using groupby and agg in the following manner:
我以下列方式使用 groupby 和 agg:
df.groupby('group')['a'].agg({'mean' : np.mean, 'std' : np.std})
and I would like to also count the values above zero in the same column ['a']
我还想计算同一列中高于零的值 ['a']
the following line does the count as I want,
以下行按照我的意愿进行计数,
sum(x > 0 for x in df['a'])
but I can't get it work when applying to groupby.
但是我在申请 groupby 时无法使用它。
Following an example for applying a pandas calculation to a groupby I tried:
以下是我尝试将Pandas计算应用于 groupby 的示例:
df.groupby('group')['a'].apply(sum(x > 0 for x in df['a']))
but I get an error message: AttributeError: 'numpy.int32' object has no attribute 'module'
但我收到一条错误消息:AttributeError: 'numpy.int32' object has no attribute ' module'
Can anybody please suggest how this might be done?
有人可以建议如何做到这一点吗?
回答by Reblochon Masque
Answer from the comments:
从评论中回答:
.agg({'pos':lambda ts: (ts > 0).sum()}) # – behzad.nouri Mar 31 at 0:00
This is my contribution to the backlog of unanswered questions :) Credits to behzad.nouri
这是我对未回答问题的积压的贡献:) 感谢 behzad.nouri
Update 2020In the latest pandas version, you need to do the following:
Update 2020在最新的 pandas 版本中,您需要执行以下操作:
.agg(pos=lambda ts: (ts > 0).sum())
otherwise it will result in the following error:
否则会导致如下错误:
SpecificationError: nested renamer is not supported

