Home > database >  Python pandas: How to match data between two dataframes
Python pandas: How to match data between two dataframes

Time:02-06

The first dataframe(df1) is similar to this:

Result A B C
2021-12-31 False True True
2022-01-01 False False True
2022-01-02 False True False
2022-01-03 True False True

df2 is an updated version of df1, the date data are new and the column names may be increased, which is similar to this:

Result A B C D
2022-01-04 False False True True
2022-01-05 True False True True
2022-01-06 False True False True
2022-01-07 False False True True

I want to integrate two databases, but I don't know how to do it。 I want to get a result similar to the following:

Result A B C D
2021-12-31 False True True NaN
2022-01-01 False False True NaN
2022-01-02 False True False NaN
2022-01-03 True False True NaN
2022-01-04 False False True True
2022-01-05 True False True True
2022-01-06 False True False True
2022-01-07 False False True True

Thank you very much!

CodePudding user response:

Use the concatenate function while ignoring indexes

df_new = pd.concat([df1, df2], ignore_index=True)

Any missing values will be 'NaN'.

  • Related