简体   繁体   English

合并表与 pandas 中的索引列

[英]Merge tables with index column in pandas

I need to join two tables:我需要加入两个表:

在此处输入图像描述

在此处输入图像描述

I use the next code:我使用下一个代码:

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

But it doesn´t work.但它不起作用。 I have obtained duplicated values.我获得了重复的值。

The result i need to obtain is:我需要得到的结果是:

user-reference_id用户参考 ID reference_date参考日期 first_purchase first_purchase month
159 159 2019-06-14 2019-06-14 62.95 62.95 6 6
5009 5009 2017-10-19 2017-10-19 58.50 58.50 10 10
5026 5026 2017-07-04 2017-07-04 35.52 35.52 7 7
5032 5032 2017-01-02 2017-01-02 71.68 71.68 1 1

Edit: I misunderstood your case so I have changed my answer.编辑:我误解了你的情况,所以我改变了我的答案。

The type of merge to be performed is wrong.要执行的合并类型错误。 You need to change your keyword argument on the how parameter.您需要在how参数上更改关键字参数。 Change it to:将其更改为:

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

There are a couple of links that are related eg :有几个相关的链接,例如:

Difference(s) between merge() and concat() in pandas pandas 中的 merge() 和 concat() 之间的差异

Pandas/Python: How to concatenate two dataframes without duplicates? Pandas/Python:如何连接两个没有重复的数据帧?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM