假设我有:

edu_data = [['school', 1, 2], ['college', 3, 4], ['grad-school', 5, 6]]
edu = pd.DataFrame(edu_data, columns = ['Education', 'StudentID1', 'StudentID2'])
print(edu)
     Education  StudentID1  StudentID2
0       school         1         2
1      college         3         4
2  grad-school         5         6

然后我有另一张桌子上面有学生证:
data = [['tom', 3], ['nick', 5], ['juli', 6], ['jack', 10]]
df = pd.DataFrame(data, columns = ['Name', 'StudentID'])
print(df)
   Name  StudentID
0   tom        3
1  nick        5
2  juli        6
3  jack       10

我如何得到一个表,在其中我将df['StudentID']与edu['StudentID1']或edu['StudentID2']匹配。如果df['StudentID']等于任何一个,那么我想将edu['Education']附加到df。
所以我希望我的输出是:
   Name  StudentID  Education
0   tom        3     college
1  nick        5     grad-school
2  juli        6     grad-school
3  jack       10     NaN

最佳答案

使用map,类似于我今天早些时候的回答

mapper = edu.set_index('StudentID1')['Education'].to_dict()
mapper.update(edu.set_index('StudentID2')['Education'].to_dict())

df['Education'] = df['StudentID'].map(mapper)


    Name    StudentID   Education
0   tom     3           college
1   nick    5           grad-school
2   juli    6           grad-school
3   jack    10          NaN

关于python - 如何基于“或”或“比较”合并2个数据框?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58458350/

10-11 19:38