[英]How to combine two data frames using merge or VLOOKUP functionality in Pandas
I have two data frame:我有两个数据框:
df1
EmID Employee Name
0 12345 Frist Person
1 35658 Second Person
2 65865 Third Person
3 28568 Foo Person
4 26699 Boo Person
df2
Manager ID Subordinate ID
1 28568 35658
2 12466 12358
3 35658 12345
I want to merge this two data frames so that my output should looks like:我想合并这两个数据框,以便我的输出看起来像:
Manger Name Emp Name
1 Foo Person Second Person
I have used df2.merge(df1, left_on = ManagerID, right_on = EmID)
but I didn't get what I am looking for.我使用过
df2.merge(df1, left_on = ManagerID, right_on = EmID)
但我没有得到我想要的。 I have also try to use for
loop still doesn't give me the desired out put.我也尝试使用
for
循环仍然没有给我想要的输出。 Any idea?任何的想法? Thanks :)
谢谢 :)
Assuming all the id's in df2
are also present in df1
, we can set the index of df1
to EmpID
and select Employee Name
column to create replacement series, then use DataFrame.replace
to substitute the values in df2
.假设
df2
中的所有 id 也存在于df1
,我们可以将df1
的索引设置为EmpID
并选择Employee Name
列以创建替换系列,然后使用DataFrame.replace
替换df2
的值。
df3 = df2.replace(df1.set_index('EmID')['Employee Name'])
df3.columns = ['Manager name', 'Emp name']
>>> df3
Manager name Emp name
1 Foo Person Second Person
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.