我需要计算以乘以Wgt列中的新值指示的某个频率重置的累积乘积.
例如,在由以下对象产生的DataFrame中:
df = pd.DataFrame(np.random.lognormal(0,0.01,27),pd.date_range('2019-01-06','2019-02-01'),columns=['Chg'])
df['Wgt'] = df['Chg'].asfreq('W')
df.loc[df.Wgt > 0,'Wgt'] = np.random.uniform(0.5,1,df.Wgt.count())
Chg Wgt
2019-01-06 1.014571 0.861546
2019-01-07 1.018993 NaN
2019-01-08 1.017461 NaN
2019-01-09 1.003788 NaN
2019-01-10 1.014106 NaN
2019-01-11 0.995758 NaN
2019-01-12 0.989058 NaN
2019-01-13 0.995897 0.602225
2019-01-14 1.007336 NaN
2019-01-15 1.004143 NaN
...
我想计算一个新列Agg,其值为:
>如果df.Wgt!= np.nan,则df.Agg = df.Wgt
>其他df.Agg = df.Agg.shift()* df.Chg
也就是说,在此示例中,Agg为:
Chg Wgt Agg
1/6/2019 1.014571 0.861546 0.861546
1/7/2019 1.018993 NaN 0.877909343
1/8/2019 1.017461 NaN 0.893238518
1/9/2019 1.003788 NaN 0.896622106
1/10/2019 1.014106 NaN 0.909269857
1/11/2019 0.995758 NaN 0.905412734
1/12/2019 0.989058 NaN 0.895505708
1/13/2019 0.995897 0.602225 0.602225
1/14/2019 1.007336 NaN 0.606642923
1/15/2019 1.004143 NaN 0.609156244
...
有什么令人讨厌的方式做到这一点?
最佳答案
在cumprod上使用np.where
s=df.loc[df.Wgt.isnull(),'Chg'].groupby(df.Wgt.notna().cumsum()).cumprod()
np.where(df.Wgt.notna(),df.Wgt,s*df.Wgt.ffill())
Out[531]:
array([0.861546,0.87790934,0.89323852,0.89662211,0.90926986,0.90541273,0.89550571,0.602225,0.60664292,0.60915624])