简体   繁体   中英

How to bring time series data in following format for sequence to sequence prediction

I have data as shown below,

在此处输入图片说明

I want it to be in following format

在此处输入图片说明

Using Python 3 pandas dataframe.

Use shift in loop with f-string s:

#python 3.6+
for i in range(1,5):
    df[f'demand_{i}'] = df['demand'].shift(-i)

#python bellow 3.6
for i in range(1,5):
    df['demand_{}'.format(i)] = df['demand'].shift(-i)

Sample :

df = pd.DataFrame({
         'demand':[4,7,8,3,5,0],

})

for i in range(1,5):
    df['demand_{}'.format(i)] = df['demand'].shift(-i)

print(df)
   demand  demand_1  demand_2  demand_3  demand_4
0       4       7.0       8.0       3.0       5.0
1       7       8.0       3.0       5.0       0.0
2       8       3.0       5.0       0.0       NaN
3       3       5.0       0.0       NaN       NaN
4       5       0.0       NaN       NaN       NaN
5       0       NaN       NaN       NaN       NaN

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