gpt4 book ai didi

python - pandas 数据框中几行的平均值

转载 作者:行者123 更新时间:2023-12-01 06:36:27 24 4
gpt4 key购买 nike

我的计算方法 (SSA) 有一个相当大的数据框 - 大约 80000 行。我想按几行(例如 20 行)对数据进行平均。我怎样才能做到这一点?

我有一个数据框,例如:

 1. 00h         03h         06h         09h         12h
10 0.003546 0.000885 0.006852 0.00171 0.001708
11 0.00667 0.012603 0.012933 0.05603 0.025855
12 0.089116 0.054549 0.022177 0.090342 0.070226
13 0.28974 0.246415 0.297231 0.399953 0.287122

最后,我想要这样的东西:

this

我该怎么做?

最佳答案

使用由 DataFrame 长度创建的范围的整数除法 numpy.arange和聚合平均值:

df = df.groupby(np.arange(len(df))//2).mean()
print (df)

00h 03h 06h 09h 12h
0 0.005108 0.006744 0.009893 0.028870 0.013782
1 0.189428 0.150482 0.159704 0.245147 0.178674

关于python - pandas 数据框中几行的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59646502/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com