问题
I want to shift to the right an entire column, fill with NAN the first column and drop the last column:
df0: A B C D
2013-12-31 10 6 6 5
2014-01-31 11 7 5 5
2014-02-28 15 8 8 8
into
df1: A B C D
2013-12-31 NaN 10 6 6
2014-01-31 NaN 11 7 5
2014-02-28 NaN 15 8 8
My solution is to split the columns into Series and then recompose the dataframe. Is there a more efficient way?
回答1:
shift shift the data by rows. Here's my trick
df.T.shift().T
Update
It's a bug when passing axis as 1. It's been solved in the current develop version. If you are living in the cutting edge, use
df.shift(axis=1)
instead.
来源:https://stackoverflow.com/questions/23105197/shift-entire-column-on-a-pandas-dataframe