pythongroupby求某一列均值_Pandas groupby按列值扩展平均值

我对熊猫不太熟悉,有点不知所措。我有一个从csv导入的数据帧,它(经过大量简化)如下所示:date = ['2013-08-10','2013-08-10','2013-08-10','2013-08-10','2013-08-10',

'2013-08-10','2013-08-10','2013-08-10','2013-08-10','2013-08-10']

event = ['213','213','213','213','214','214','214','215','215','215']

side = ['A','B','B','B','A','B','A','B','A','B',]

value = [0.193,0.193,0.092,0.027,0.027,0.058,0.027,0.079,0.193,0.159]

df = pd.DataFrame(zip(event,date,side,value),

columns=['event','date','side','value'])

event date side value

0 213 2013-08-10 A 0.193

1 213 2013-08-10 B 0.193

2 213 2013-08-10 B 0.092

3 213 2013-08-10 B 0.027

4 214 2013-08-10 A 0.027

5 214 2013-08-10 B 0.058

6 214 2013-08-10 A 0.027

7 215 2013-08-10 B 0.079

8 215 2013-08-10 A 0.193

9 215 2013-08-10 B 0.159

我想要的是对每个事件的每一个边对应的值求和。我用groupby实现了这一点:

^{pr2}$

但我还想添加一个新的列,每边的平均值都在扩大,如下所示:value

event side roll_mean

213 A 0.193 0

B 0.312 0

214 A 0.054 0.193

B 0.058 0.312

215 A 0.193 0.124

B 0.238 0.185

请注意,每个事件都有两面性,但并不总是A和B。我想要的是类似excel的东西平均值。如果函数,该函数计算当前边的所有值的扩展平均值,并应用于前面的所有行。如有任何帮助,我们将不胜感激。在

已标记关键词 清除标记
表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页