Following on from this question Python custom function using rolling_apply for pandas, about using rolling_apply. Although I have progressed with my function, I am struggling to deal with a function that requires two or more columns as inputs:
Creating the same setup as before
import pandas as pd
import numpy as np
import random
tmp = pd.DataFrame(np.random.randn(2000,2)/10000,
index=pd.date_range(‘2001-01-01’,periods=2000),
columns=[‘A’,’B’])
But changing the function slightly to take two columns.
def gm(df,p):
df = pd.DataFrame(df)
v =((((df[‘A’]+df[‘B’])+1).cumprod())-1)*p
return v.iloc[-1]
It produces the following error:
pd.rolling_apply(tmp,50,lambda x: gm(x,5))
KeyError: u’no item named A’
I think it is because the input to the lambda function is an ndarray of length 50 and only of the first column, and doesn’t take two columns as the input. Is there a way to get both columns as inputs and use it in a rolling_apply function.
Again any help would be greatly appreciated…
解决方案
Workaround based on using aux column ii which is used to select window inside of manipulating function gm:
import pandas as pd
import numpy as np
import random
tmp = pd.DataFrame(np.random.randn(2000,2)/10000, columns=[‘A’,’B’])
tmp[‘date’] = pd.date_range(‘2001-01-01’,periods=2000)
tmp[‘ii’] = range(len(tmp))
def gm(ii, df, p):
x_df = df.iloc[map(int, ii)]
print x_df
v =((((x_df[‘A’]+x_df[‘B’])+1).cumprod())-1)*p
print v
return v.iloc[-1]
print tmp.head()
res = pd.rolling_apply(tmp.ii, 50, lambda x: gm(x, tmp, 5))
print res
Original: https://blog.csdn.net/weixin_30218733/article/details/113030713
Author: 博雅汇MBA申请中心
Title: pandas rolling方法_Python Pandas rolling_apply将两列输入功能
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/741572/
转载文章受原作者版权保护。转载请注明原作者出处!