假设我有:
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
然后我有另一个表,其中包含人们的学生 ID:
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
s = edu.set_index('Education').stack().reset_index(level=1, drop=True)
df['Education'] = df.StudentID.map(pd.Series(s.index, s.values))
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/