0

I have two dataframes that look like this:

dataframe1

               col1      value
date   
2021-09-14      1        0.133
2022-03-09      1        0.452
2022-01-20      1        0.891
2021-12-25      1        0.321

dataframe2

               col1      value
date   
2021-09-05      1        0.129
2022-03-12      1        0.546
2022-01-17      1        0.725
2021-12-10      1        0.492

*the values in col 1 are always the same (and also same between dataframes). *the date is index

I would like to create new dataframe that will have values and dates from both columns as one, like this:

                value
date   
2021-09-05      0.129
2021-09-14      0.133
2022-03-12      0.546
2022-01-17      0.725
2021-12-10      0.492
2022-03-09      0.452
2022-01-20      0.891
2021-12-25      0.321

At the moment in order to do that I make list from each dataframe of the values and of the dates, and then I make new dataframe, like this:

values=dataframe1['values'].tolist()+dataframe2['values'].tolist()
times=[str(x) for x in dataframe1.index.tolist()]+[str(x) for x in dataframe2.index.tolist()]

harmony=pd.DataFrame(zip(times,values),columns=['time','Combined Dataset'])
harmony['time']=pd.to_datetime(harmony['time'],format='%Y-%m-%d')
harmony=harmony.set_index('time')

My question if there is any nicer/smarter/more pythonic way to do that.

Reut
  • 1,447
  • 1
  • 12
  • 37
  • Does the order matter? You can try using [`concat`](https://pandas.pydata.org/docs/reference/api/pandas.concat.html) – Shaido Apr 27 '22 at 08:22

0 Answers0