pandas Groupby AGG-如何获得计数? [英] Pandas groupby agg - how to get counts?
本文介绍了 pandas Groupby AGG-如何获得计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试获取指标的总和,均值和计数
I am trying to get sum, mean and count of a metric
df.groupby(['id', 'pushid']).agg({"sess_length": [ np.sum, np.mean, np.count]})
但是我得到模块'numpy'没有属性'count',我尝试了多种表达count函数的方法,但无法使其正常工作。我如何只将一个记录总数与其他指标一起计算?
But I get "module 'numpy' has no attribute 'count'", and I have tried different ways of expressing the count function but can't get it to work. How do I just an aggregate record count together with the other metrics?
推荐答案
您可以使用字符串代替函数,就像这样:
You can use strings instead of the functions, like so:
In [16]: df = pd.DataFrame({"id": list("ccdef"),
"pushid": list("aabbc"),
"sess_length": [10, 20, 30, 40, 50]})
In [17]: df.groupby(['id', 'pushid']).agg({"sess_length": [ 'sum', 'mean', 'count']})
Out[17]: sess_length
sum mean count
id pushid
c a 30 15 2
d b 30 30 1
e b 40 40 1
f c 50 50 1
这篇关于 pandas Groupby AGG-如何获得计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文