Variable shift in Pandas

无人久伴 提交于 2019-12-23 20:13:19

问题


having two columns A and B in a dataframe:

   A   B
0  1   6
1  2   7
2  1   8
3  2   9
4  1  10

I would like to create a column C. C must have values of B shifted by value of A:

   A   B   C 
0  1   6 NaN
1  2   7 NaN
2  1   8   7
3  2   9   7
4  1  10   9

The command:

df['C'] = df['B'].shift(df['A'])

does not work. Do you have any other ideas?


回答1:


This is tricky due to index alignment, you can define a user func and apply row-wise on your df, here the function will perform a shift on the B column and return the index value (using .name attribute to return the index) of the shifted column:

In [134]:    
def func(x):
    return df['B'].shift(x['A'])[x.name]
df['C'] = df.apply(lambda x: func(x), axis=1)
df

Out[134]:
   A   B    C
0  1   6  NaN
1  2   7  NaN
2  1   8  7.0
3  2   9  7.0
4  1  10  9.0



回答2:


I'd use help from numpy to avoid the apply

l = np.arange(len(df)) - df.A.values
df['C'] = np.where(l >=0, df.B.values[l], np.nan)
df

   A   B    C
0  1   6  NaN
1  2   7  NaN
2  1   8  7.0
3  2   9  7.0
4  1  10  9.0

simple time test



来源:https://stackoverflow.com/questions/38523920/variable-shift-in-pandas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!