这是一种方法
In [19]: def cust_mean(grp): ....: grp['mean'] = grp['option_value'].mean() ....: return grp ....:In [20]: o2.groupby(['YEAR', 'daytype', 'hourtype']).apply(cust_mean)Out[20]: YEAR daytype hourtype scenario option_value mean0 2015 SAT of_h 0 0.134499 28.2829461 2015 SUN of_h 1 63.019250 63.0192502 2015 WD of_h 2 52.113516 52.1135163 2015 WD pk_h 3 43.126513 43.1265134 2015 SAT of_h 4 56.431392 28.282946
那么,您的尝试出了什么问题?
它返回形状与原始数据帧不同的聚合。
In [21]: o2.groupby(['YEAR', 'daytype', 'hourtype'])['option_value'].mean()Out[21]:YEAR daytype hourtype2015 SAT of_h 28.282946 SUN of_h 63.019250 WD of_h 52.113516 pk_h 43.126513Name: option_value, dtype: float64
或 使用
transform
In [1461]: o2['premium'] = (o2.groupby(['YEAR', 'daytype', 'hourtype'])['option_value'] .transform('mean'))In [1462]: o2Out[1462]: YEAR daytype hourtype scenario option_value premium0 2015 SAT of_h 0 0.134499 28.2829461 2015 SUN of_h 1 63.019250 63.0192502 2015 WD of_h 2 52.113516 52.1135163 2015 WD pk_h 3 43.126513 43.1265134 2015 SAT of_h 4 56.431392 28.282946
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)