How do I shift multiple columns? Pandas, Python

后端 未结 1 1671
太阳男子
太阳男子 2020-12-11 05:08

For simplicity sake, lets say I have this dataframe.

Date           Open    Close
2016-01-01     100     129
2016-01-02     198     193
2016-01-03     103          


        
相关标签:
1条回答
  • 2020-12-11 05:53

    You could do it like this creating a mask for columns to exclude:

    mask = ~(df.columns.isin(['Date','Close']))
    
    cols_to_shift = df.columns[mask]
    
    df[cols_to_shift] = df.loc[:,mask].shift(-1)
    

    OR

    df[cols_to_shift] = df[cols_to_shift].shift(-1)
    

    Output:

             Date   Open  Close
    0  2016-01-01  198.0    129
    1  2016-01-02  103.0    193
    2  2016-01-03  102.0    102
    3  2016-01-04    NaN    109
    
    0 讨论(0)
提交回复
热议问题