Home > Net >  Extend a dataframe with values and next dates
Extend a dataframe with values and next dates

Time:05-16

I have to following dataframe:

        value        time
1  177.059998  2022-05-13

And this array of numbers:

[[192.62466]
 [189.60422]
 [186.8717 ]
 [191.22443]]

I want to create new rows in the dataframe with this values and forward date values like so:

        value        time
1  177.059998  2022-05-13
2  192.62466   2022-05-14
3  189.60422   2022-05-15
4  186.8717    2022-05-16
5  191.22443   2022-05-17

CodePudding user response:

You can use:

# Assuming 'time' column is a DatetimeIndex
df['time'] = pd.to_datetime(df['time'])

df1 = pd.DataFrame({'value': np.array(m).flatten(),
                    'time': pd.date_range(df['time'].max(), periods=len(m) 1, 
                                          freq='D', inclusive='right')})

out = pd.concat([df, df1], ignore_index=True)

Output:

>>> out
        value       time
0  177.059998 2022-05-13
1  192.624660 2022-05-14
2  189.604220 2022-05-15
3  186.871700 2022-05-16
4  191.224430 2022-05-17

CodePudding user response:

import pandas as pd
data = {'Value': [1,2,3,4,5]}
data=pd.DataFrame(data=data)
data1=pd.date_range(start='2022-05-13',end='2022-05-17',name='date')
data1=pd.DataFrame(data=data1)
pd.concat([data,data1], axis=1)

result

    Value   date
0   1   2022-05-13
1   2   2022-05-14
2   3   2022-05-15
3   4   2022-05-16
4   5   2022-05-17
  • Related