【问题标题】:Merge tables with index column in pandas将表与熊猫中的索引列合并
【发布时间】:2021-07-31 23:23:16
【问题描述】:

我需要加入两个表:

我使用下一个代码:

    table = pd.merge(df1, df2, on=['user-reference_id'], how = 'right')

但它不起作用。我得到了重复的值。

我需要得到的结果是:

user-reference_id reference_date first_purchase month
159 2019-06-14 62.95 6
5009 2017-10-19 58.50 10
5026 2017-07-04 35.52 7
5032 2017-01-02 71.68 1

【问题讨论】:

  • user-reference_id 是否在您的一个或两个数据框中多次出现?如果是这样,那么您将获得记录的笛卡尔积。
  • 我在两个数据框中都有 user-reference_id,我需要通过 user-reference_id 加入两个表。在最后一张表中,我有 reference_date 作为索引,当我加入两个表时,我得到了重复

标签: python pandas join merge multi-index


【解决方案1】:

编辑:我误解了你的情况,所以我改变了答案。

要执行的合并类型错误。您需要更改 how 参数上的关键字参数。改成:

table = pd.merge(df1, df2, on=['user-reference_id'], how='outer')

有几个相关的链接,例如:

Difference(s) between merge() and concat() in pandas

Pandas/Python: How to concatenate two dataframes without duplicates?

https://pandas.pydata.org/pandas-docs/stable/user_guide/merging.html#

【讨论】:

猜你喜欢
  • 2021-09-01
  • 2012-12-18
  • 2016-08-01
  • 2020-04-05
  • 2018-06-17
  • 2018-02-03
  • 2020-10-26
  • 2018-02-02
  • 1970-01-01
相关资源
最近更新 更多