[英]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![]() |
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 ![]() |
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.