使用 pandas GroupBy 获取每个组的统计信息(例如计数、平均值等)? [英] Get statistics for each group (such as count, mean, etc) using pandas GroupBy?
问题描述
我有一个数据框 df
并且我使用其中的几列到 groupby
:
I have a data frame df
and I use several columns from it to groupby
:
df['col1','col2','col3','col4'].groupby(['col1','col2']).mean()
通过上面的方式,我几乎得到了我需要的表格(数据框).缺少的是包含每个组中行数的附加列.换句话说,我的意思是但我也想知道使用了多少个数字来获得这些平均值.例如,第一组有 8 个值,第二组有 10 个,依此类推.
In the above way I almost get the table (data frame) that I need. What is missing is an additional column that contains number of rows in each group. In other words, I have mean but I also would like to know how many number were used to get these means. For example in the first group there are 8 values and in the second one 10 and so on.
简而言之:如何获取数据帧的分组统计信息?
In short: How do I get group-wise statistics for a dataframe?
推荐答案
在 groupby
对象上,agg
函数可以带一个列表到 一次应用多种聚合方法.这应该会给你你需要的结果:
On groupby
object, the agg
function can take a list to apply several aggregation methods at once. This should give you the result you need:
df[['col1', 'col2', 'col3', 'col4']].groupby(['col1', 'col2']).agg(['mean', 'count'])
这篇关于使用 pandas GroupBy 获取每个组的统计信息(例如计数、平均值等)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!