[英]dataframe compound calculation on aggregations
import pandas as pd
times = pd.to_datetime(pd.Series(['2014-07-4',
'2014-07-15','2014-08-25','2014-08-25','2014-09-10','2014-09-15']))
strategypercentage = [0.01, 0.02, -0.03, 0.04,0.5,-0.3]
df = pd.DataFrame({'Strategy': strategypercentage}, index=times)
##lambda x: ((1+x).cumprod()-1)
df.resample("1M").agg({'Strategy' : ['sum','prod']})
數據框包含日期和每日百分比變化,我如何計算復合的每月百分比變化(看起來像((1 + x).cumprod()-1))-但是如何使用agg實施? 預期結果: 在此處輸入圖片描述
看來您需要:
df['new'] = df.resample("1M")['Strategy'].apply(lambda x: ((1+x).cumprod()-1))
print (df)
Strategy new
2014-07-04 0.01 0.0100
2014-07-15 0.02 0.0302
2014-08-25 -0.03 -0.0300
2014-08-25 0.04 0.0088
2014-09-10 0.50 0.5000
2014-09-15 -0.30 0.0500
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.