本文介绍了Python:摘要和聚合DataFrame中的组和子组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试建立一个表,该表具有按子组划分的组,每个子组具有计数和平均值。例如,我要转换以下数据框:
I am trying to build a table that has groups that are divided by subgroups with count and average for each subgroup. For example, I want to convert the following data frame:
对于这样的表,其中 interval 是一个更大的组,而 a thru i 列成为该组内的子组,每个单元格中都有对应的子组的计数和平均值:
To a table that looks like this where the interval is a bigger group and columns a thru i become subgroups within the group with the corresponding subgroups' count and average in each cell:
我尝试了此尝试,但没有成功:
I have tried this with no success:
推荐答案
使用与和具有新列名称的聚合函数的元组:
Use DataFrame.melt
with GroupBy.agg
and tuples for aggregate functions with new columns names:
df1 = (df.melt('interval', var_name='source')
.groupby(['interval','source'])['value']
.agg([('cnt','count'), ('average','mean')])
.reset_index())
print (df1.head())
interval source cnt average
0 0 a 1 5.0
1 0 b 1 0.0
2 0 c 1 0.0
3 0 d 1 0.0
4 0 f 1 0.0
这篇关于Python:摘要和聚合DataFrame中的组和子组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!