shift 函数

 

DataFrame.shift(periods=1, freq=None, axis=0)

 

参数

  • periods:类型为int,表示移动的幅度,可以是正数,也可以是负数,默认值是1,1就表示移动一次,注意这里移动的都是数据,而索引是不移动的,移动之后没有对应值的,就赋值为NaN。
  • freq: DateOffset, timedelta, or time rule string,可选参数,默认值为None,只适用于时间序列,如果这个参数存在,那么会按照参数值移动时间索引,而数据值没有发生变化。
  • axis:{0, 1, ‘index’, ‘columns’},表示移动的方向,如果是0或者’index’表示上下移动,如果是1或者’columns’,则会左右移动。

 

假如现在有一个DataFrame数据df,如下所示:

indexvalue1
A 0
B 1
C 2
D 3

执行代码:

df.shift()
indexvalue1
A NaN
B 0
C 1
D 2

 

执行代码:

df.shift(2)
indexvalue1
A NaN
B NaN
C 0
D 1

 

执行代码:

df.shift(-1)
indexvalue1
A 1
B 2
C 3
D NaN

 

df1:

indexvalue1
2016-06-01 0
2016-06-02 1
2016-06-03 2
2016-06-04 3

执行代码:

df1.shift(periods=1,freq=datetime.timedelta(1))

 

indexvalue1
2016-06-02 0
2016-06-03 1
2016-06-04 2
2016-06-05 3

posted on 2017-05-22 17:58  trp  阅读(546)  评论(0编辑  收藏  举报

导航