0

I have two dataframes df1 and df2 (with some dates which are not intersecting). I would like to merge them into one bigger dataframe containing all the (5) dates from initial dataframe (= 5 lines). Thanks in advance!

import pandas as pd

df1 = pd.DataFrame({"date": ['2021-3-22', '2021-4-7', '2021-4-18', '2021-5-12'],
"x": [3, 3, 3, 0 ]})
df1.set_index('date', inplace=True)
df1

date        x   
2021-3-22   3
2021-4-7    3
2021-4-18   3
2021-5-12   0


df2 = pd.DataFrame({"date": ['2021-3-22', '2021-4-8', '2021-4-18', '2021-5-12'],
"y": [3, 3, 3, 0 ]})
df2.set_index('date', inplace=True)
df2

date        y
2021-3-22   3
2021-4-8    3
2021-4-18   3
2021-5-12   0

----------------------------
Desired result:
date        x   y
2021-3-22   3   3
2021-4-7    3   NaN
2021-4-8    NaN 3
2021-4-18   3   3
2021-5-12   0   0


Champion
  • 93
  • 6

1 Answers1

0

You can do join

df = df1.join(df2,how='outer')
df
Out[267]: 
             x    y
date               
2021-3-22  3.0  3.0
2021-4-18  3.0  3.0
2021-4-7   3.0  NaN
2021-4-8   NaN  3.0
2021-5-12  0.0  0.0
BENY
  • 317,841
  • 20
  • 164
  • 234