[英]How do I create a new df by replace the df ID# with names of another df?
I have to DFs one contains just ID# and the other is a more comprehensive DF with names, salary, ages and ID#'s 我必须使用DF,一个仅包含ID#,另一个是更全面的DF,其中包含姓名,薪水,年龄和ID#
df1 looks like this df1看起来像这样
Name Salary ID Age City
Sam 52000 542 52 NYC
Bob 15000 451 21 LA
Sam 72000 556 21 SF
where df 2 looks like this with the ID# df 2的ID#如下所示
Index 1 2 3 4
a 542 352 581 521
b 451 215 556 451
c 540 332 511 121
d 451 515 156 951
Note some of the ID# reap and that is expected 注意一些ID#收获,这是预期的
How do get df2 but just replacing them with the names (and some do repeat I do not want to drop them) 如何获得df2,但仅将其替换为名称(有些重复,我不想删除它们)
My goal was to have a new df3 where it looked like this 我的目标是制作一个新的df3
df3 DF3
Index 1 2 3 4
a Sam Bill Le Sam
b Mike Jane Kevin Le
c Jame Kerry David Mike
d Andy Steve Jane Andy
(note my examples id# does not match the names please forgive me) (请注意我的示例ID#与名称不匹配,请原谅我)
You can fist build a lookup dict and then use applymap 您可以先建立一个查询字典,然后使用applymap
name_map = df.set_index('ID')['Name'].to_dict()
df2.applymap(name_map.get)
1 2 3 4
0 Sam None None None
1 Bob None Sam Bob
2 None None None None
3 Bob None None None
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.