简体   繁体   English

仅合并 pandas dataframe 的某些列

[英]Merge only certain columns of pandas dataframe

I have a pandas dataframe that looks like this:我有一个 pandas dataframe 看起来像这样:

import pandas as pd
d = {'A': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1], 
     'B': [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 11]}
df1 = pd.DataFrame(data=d)
df1

    A   B
0   1   11
1   2   12
2   3   13
3   4   14
4   5   15
5   6   16
6   7   17
7   8   18
8   9   19
9   10  20
10  1   11

and another dataframe that looks like this:和另一个看起来像这样的 dataframe:

import pandas as pd
d = {'C': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20], 
     'D': [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30],
     'id': [21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38 ,39, 40]}
df2 = pd.DataFrame(data=d)
df2

    C   D   id
0   1   11  21
1   2   12  22
2   3   13  23
3   4   14  24
4   5   15  25
5   6   16  26
6   7   17  27
7   8   18  28
8   9   19  29
9   10  20  30
10  11  21  31
11  12  22  32
12  13  23  33
13  14  24  34
14  15  25  35
15  16  26  36
16  17  27  37
17  18  28  38
18  19  29  39
19  20  30  40

I want to join the id column from df2 to df1 based on the A and B columns in df1 and the C and D columns in df2 , like so:我想根据df1中的AB列以及df2中的CD列将id列从df2连接到df1 ,如下所示:

    A   B   id
0   1   11  21
1   2   12  22
2   3   13  23
3   4   14  24
4   5   15  25
5   6   16  26
6   7   17  27
7   8   18  28
8   9   19  29
9   10  20  30
10  1   11  21

I was hoping I could achieve this by running the code below.我希望我可以通过运行下面的代码来实现这一点。 However, as expected it is giving me an KeyError: 'C' .但是,正如预期的那样,它给了我一个KeyError: 'C' Is there a more elegant way to achieve this?有没有更优雅的方法来实现这一点?

df_merge = pd.merge(df1, df2['id'], left_on=['A', 'B'], right_on=['C', 'D'], how='left')

Select necessary columns with id in list and use rename : Select 列表中带有id的必要列并使用rename

d= {'C':'A','D':'B'}
df_merge = pd.merge(df1, df2[['id', 'C', 'D']].rename(columns=d), on=['A', 'B'], how='left')

In your solution use list and then drop columns C, D :在您的解决方案中使用列表,然后删除列C, D

df_merge = (pd.merge(df1, df2[['id', 'C', 'D']], 
                   left_on=['A', 'B'], right_on=['C', 'D'], how='left')
              .drop(['C','D'], axis=1))

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

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