Shift entire column on a pandas dataframe

ε祈祈猫儿з 提交于 2019-12-24 13:56:03

问题


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

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