📅  最后修改于: 2020-10-29 02:11:43             🧑  作者: Mango
Pandas merge()定义为以下过程:将两个数据集合为一个,并根据公共属性或列对齐行。它是DataFrame对象之间所有标准数据库联接操作的入口点:
pd.merge(left, right, how='inner', on=None, left_on=None, right_on=None,
left_index=False, right_index=False, sort=True)
# import the pandas library
import pandas as pd
left = pd.DataFrame({
'id':[1,2,3,4],
'Name': ['John', 'Parker', 'Smith', 'Parker'],
'subject_id':['sub1','sub2','sub4','sub6']})
right = pd.DataFrame({
'id':[1,2,3,4],
'Name': ['William', 'Albert', 'Tony', 'Allen'],
'subject_id':['sub2','sub4','sub3','sub6']})
print (left)
print (right)
输出量
id Name subject_id
0 1 John sub1
1 2 Parker sub2
2 3 Smith sub4
3 4 Parker sub6
id Name subject_id
0 1 William sub2
1 2 Albert sub4
2 3 Tony sub3
3 4 Allen sub6
import pandas as pd
left = pd.DataFrame({
'id':[1,2,3,4,5],
'Name': ['Alex', 'Amy', 'Allen', 'Alice', 'Ayoung'],
'subject_id':['sub1','sub2','sub4','sub6','sub5']})
right = pd.DataFrame({
'id':[1,2,3,4,5],
'Name': ['Billy', 'Brian', 'Bran', 'Bryce', 'Betty'],
'subject_id':['sub2','sub4','sub3','sub6','sub5']})
print pd.merge(left,right,on='id')
输出量
id Name_x subject_id_x Name_y subject_id_y
0 1 John sub1 William sub2
1 2 Parker sub2 Albert sub4
2 3 Smith sub4 Tony sub3
3 4 Parker sub6 Allen sub6