如何计算批次数据框的平均值? [英] how to calculate the average of dataframe of batches?

查看:55
本文介绍了如何计算批次数据框的平均值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何计算由csv文件的batchsizes创建的数据帧的平均值?

How do I calculate the average of dataframes created by batchsizes of csv file?

Input:
A,1
B,2
B,1
C,2
A,1
B,3
B,1
C,1
A,1
B,2
B,1
C,3  

我想将它们按4行(A,B,B,C)分组,然后计算第二列的平均值.

I want to group them by 4 rows (A,B,B,C) and calculate the average of 2nd column.

output:
A, average(1,1,1)
B, average(2,3,2)
B, average(1,1,1)
C, average(1,2,3)

推荐答案

假定列的名称为 label,value ,因为您要对固定数量的行求平均,因此重塑就可以了:

Assuming the columns is named label, value, since you are averaging over a fixed number of rows, a reshape would be fine:

nrows=4

pd.DataFrame({'label': df['label'].iloc[:nrows],
              'value':df['value'].values.reshape(-1,nrows).mean(axis=0)
})

输出:

  label     value
0     A  1.000000
1     B  2.333333
2     B  1.000000
3     C  2.000000

这篇关于如何计算批次数据框的平均值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆