[英]How to shift columns in pandas df left based on value in another column?
在每一行上使用shift
:
df.iloc[:, 1:] = df.apply(lambda x: x.iloc[1:].shift(-x.loc['Shift']),
axis=1, result_type='expand')
print(df)
# Output
Shift Col_1 Col_2 Col_3 Col_4 Col_5 Col_6 Col_7 Col_8 Col_9 Col_10
0 5 6.0 7.0 8.0 9.0 10.0 NaN NaN NaN NaN NaN
1 3 4.0 5.0 6.0 7.0 8.0 9.0 10.0 NaN NaN NaN
2 2 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 NaN NaN
3 4 5.0 6.0 7.0 8.0 9.0 10.0 NaN NaN NaN NaN
4 1 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 NaN
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.