How to create a lagged data structure using pandas dataframe

后端 未结 8 2255
耶瑟儿~
耶瑟儿~ 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:16

    Here is a cool one liner for lagged features with _lagN suffixes in column names using pd.concat:

    lagged = pd.concat([s.shift(lag).rename('{}_lag{}'.format(s.name, lag+1)) for lag in range(3)], axis=1).dropna()
    

提交回复
热议问题