熊猫数据框条件.mean()取决于特定列中的值

熊猫数据框条件.mean()取决于特定列中的值,第1张

熊猫数据框条件.mean()取决于特定列中的值

这是一种方法

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


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5648964.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存