How to create a lagged data structure using pandas dataframe

后端 未结 8 2249
耶瑟儿~
耶瑟儿~ 2020-12-04 15:24

Example

s=pd.Series([5,4,3,2,1], index=[1,2,3,4,5])
print s 
1    5
2    4
3    3
4    2
5    1

Is there an efficient way to create a serie

8条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-04 16:14

    For a dataframe df with the lag to be applied on 'col name', you can use the shift function.

    df['lag1']=df['col name'].shift(1)
    df['lag2']=df['col name'].shift(2)
    

提交回复
热议问题