简体   繁体   English

在pandas数据帧上移动整个列

[英]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: 我想向右移动整个列,在第一列填充NAN并删除最后一列:

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. 我的解决方案是将列拆分为Series,然后重新组合数据帧。 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. 当将轴传递为1时,这是一个错误。它已在当前的开发版本中得到解决。 If you are living in the cutting edge, use 如果你生活在最前沿,请使用

df.shift(axis=1)

instead. 代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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