查找 pandas 连续两行的平均值 [英] Finding the average of two consecutive rows in pandas
本文介绍了查找 pandas 连续两行的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试查找每列中连续两行的平均值
I am trying to find the average of two consecutive rows in each column
In[207]: df = DataFrame({"A": [9, 4, 2, 1, 4], "B": [12, 7, 5, 4,8]})
In[208]: df
Out[207]:
A B
0 9 12
1 4 7
2 2 5
3 1 4
4 4 8
结果应为:
Out[207]:
A B
0 6.5 9.5
1 1.5 4.5
如果元素编号id为奇数,则丢弃最后一行.
If the number of elements id odd, discard the last row.
推荐答案
尝试一下:
In [29]: idx = len(df) - 1 if len(df) % 2 else len(df)
In [30]: df[:idx].groupby(df.index[:idx] // 2).mean()
Out[30]:
A B
0 6.5 9.5
1 1.5 4.5
这篇关于查找 pandas 连续两行的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文