How do I shift multiple columns? Pandas, Python

Viewed 9687

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     102
2016-01-04     102     109

I can't state all the column names because there are too many. So how can I shift all the columns except for two of them (Date & Close)? I want to shift all the columns except (Date & Close) back one row.

Date           Open    Close
2016-01-01     198     129
2016-01-02     103     193
2016-01-03     102     102
2016-01-04     NaN     109
2 Answers

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

Just use double brackets and insert the columns you want to shift.

df = pd.DataFrame([[1,2,3],
                   [4,5,6],
                   [7,8,9]], columns = ['a','b','c'])
>>>
   a  b  c
0  1  2  3
1  4  5  6
2  7  8  9


df[['a','b']] = df[['a','b']].shift(-1)

>>>
     a    b  c
0  4.0  5.0  3
1  7.0  8.0  6
2  NaN  NaN  9
Related