Python pandas: 如何在两个数据框之间匹配数据

huangapple go评论51阅读模式
英文:

Python pandas: How to match data between two dataframes

问题

第一个数据框(df1)类似于这样:

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是df1的更新版本,日期数据是新的,列名可能增加,类似于这样:

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

我想要整合这两个数据库,但不知道如何做。
我想要得到类似以下的结果:

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

非常感谢!

英文:

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!

答案1

得分: 0

使用concatenate函数时忽略索引

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

任何缺失的数值将会是'NaN'。

英文:

Use the concatenate function while ignoring indexes

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

Any missing values will be 'NaN'.

huangapple
  • 本文由 发表于 2023年2月6日 08:36:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356475.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定