简体   繁体   中英

Shift entire column on a pandas dataframe

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM