简体   繁体   English

如何将两个数据框连接在一起

[英]How can I join two dataframes together

My first data frame has various columns one of which contains ID column and my second data frame has various columns one of which contains a No so I have found the link between the two. 我的第一个数据帧具有各种列,其中一个包含ID列,而我的第二个数据帧具有各种列,其中一个包含No,因此我发现了两者之间的链接。 However how can I link these together using the number to assign the postcode information from data frame 2 to the correct practice in data frame 1. 但是,如何使用数字将它们链接在一起,以将数据帧2中的邮政编码信息分配给数据帧1中的正确做法。

Any help would be greatly appreciated!!! 任何帮助将不胜感激!!!

Date frame 1 日期框架1

ID  place  Items Cost
0      5     10  2001.00
1     12     2  20.98
2      2     4  100.80
3      7     7  199.60

Data frame 2 数据框2

ID   No     Dr      Postcode
0      1     Dr.K     BT94 7HX
1      5     Dr.H     BT7 4MC
2      3     Dr.Love  BT9 1HE
3      7     Dr.Kerr  BT72 4TX

I want to create a new column 'Postcode' in Data frame 1 and assign the postcode to the correct Practice 我想在数据框1中创建一个新列“邮政编码”,并将该邮政编码分配给正确的练习

ID  Place Items Cost Postcode      
0      5         10  BT7 4MC
1      2          3  BT9 1HE
2      22         8  BT62 4TU
3      7          7  BT72 4TX

How can I do this?? 我怎样才能做到这一点??

IIUC, I think what you are looking for is 'left_on' and 'right_on' parameters in merge: IIUC,我想您正在寻找的是合并中的“ left_on”和“ right_on”参数:

df1.merge(df2, left_on='Practice', right_on='Prac No')

Output: 输出:

   ID_x  Practice  Items    Cost  ID_y  Prac No       Dr  Postcode
0     0         5     10  2001.0     1        5     Dr.H   BT7 4MC
1     3         7      7   199.6     3        7  Dr.Kerr  BT72 4TX

Or another way is to use set_index and map : 或者另一种方法是使用set_indexmap

df1['Postcode'] = df1['Practice'].map(df2.set_index('Prac No')['Postcode'])
df1

Output: 输出:

   ID  Practice  Items     Cost  Postcode
0   0         5     10  2001.00   BT7 4MC
1   1        12      2    20.98       NaN
2   2         2      4   100.80       NaN
3   3         7      7   199.60  BT72 4TX

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

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