简体   繁体   English

合并多列上的数据帧 - Python

[英]MERGE dataframes on multiple columns - Python

I have two dataframes.我有两个数据框。 df1: df1:

    ID    Date                     Name      Volume Up
0   1   2019-02-01 to 2019-03-15   Call       50
1   1   2019-02-01 to 2019-03-15   Email      60.5
2   1   2019-02-01 to 2019-03-15   Radio      20
3   2   2019-02-01 to 2019-03-15   Call       5.5
4   2   2019-02-01 to 2019-03-15   Email      6.4
5   2   2019-02-01 to 2019-03-15   Radio      15

df2: df2:

    ID          Date               Name      Volume Down
0   1   2019-02-01 to 2019-03-15   Call       66
1   1   2019-02-01 to 2019-03-15   Email      50
2   1   2019-02-01 to 2019-03-15   Radio      40
3   2   2019-02-01 to 2019-03-15   Call       10
4   2   2019-02-01 to 2019-03-15   Email      12.2
5   2   2019-02-01 to 2019-03-15   Radio      7

I would like to merge both files on ID, Date and Name column.我想合并 ID、日期和名称列上的两个文件。

Currently, i am using目前,我正在使用

merged = df1.merged(df2, on=['ID','Date','Name']

But it is only returning 'Call'但它只返回“呼叫”

Expected output:预期 output:

   ID    Date                     Name      Volume Up Volume Down
0   1   2019-02-01 to 2019-03-15   Call       50         66
1   1   2019-02-01 to 2019-03-15   Email      60.5       50
2   1   2019-02-01 to 2019-03-15   Radio      20         40
3   2   2019-02-01 to 2019-03-15   Call       5.5        10
4   2   2019-02-01 to 2019-03-15   Email      6.4        12.2
5   2   2019-02-01 to 2019-03-15   Radio      15         7

What is the best way to go about this? go 关于这个的最佳方法是什么?

Update: Your new sample data still merges fine for me, so it's possible your real columns have spurious whitespace.更新:您的新样本数据对我来说仍然可以很好地合并,因此您的真实列可能有虚假的空白。 Try to strip() them before merging:在合并之前尝试strip()它们:

df1.Date = df1.Date.str.strip()
df2.Date = df2.Date.str.strip()
df1.Name = df1.Name.str.strip()
df2.Name = df2.Name.str.strip()

merged = df1.merge(df2, on=['ID', 'Date', 'Name'])

#    ID                      Date   Name  Volume Up  Volume Down
# 0   1  2019-02-01 to 2019-03-15   Call       50.0         66.0
# 1   1  2019-02-01 to 2019-03-15  Email       60.5         50.0
# 2   1  2019-02-01 to 2019-03-15  Radio       20.0         40.0
# 3   2  2019-02-01 to 2019-03-15   Call        5.5         10.0
# 4   2  2019-02-01 to 2019-03-15  Email        6.4         12.2
# 5   2  2019-02-01 to 2019-03-15  Radio       15.0          7.0

You can give merge() a list of columns to merge on :您可以为merge()提供要合并on列列表:

out = df1.merge(df2, on=['ID', 'Date', 'Name'])

#    ID        Date     Name  Volume Up  Volume Down
# 0   1  2019-01-01   Amazon       50.0         33.0
# 1   1  2019-02-01  Netflix       60.5         67.0
# 2   2  2019-01-01   Amazon        5.5          3.5
# 3   2  2019-02-01  Netflix       20.0         47.0

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

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