[英]How can I create a DF by conditionally looking up into another DF, and appending the results horizontally into a new DF?
[英]How do I create a new df by replace the df ID# with names of another df?
我必須使用DF,一個僅包含ID#,另一個是更全面的DF,其中包含姓名,薪水,年齡和ID#
df1看起來像這樣
Name Salary ID Age City
Sam 52000 542 52 NYC
Bob 15000 451 21 LA
Sam 72000 556 21 SF
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
注意一些ID#收獲,這是預期的
如何獲得df2,但僅將其替換為名稱(有些重復,我不想刪除它們)
我的目標是制作一個新的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
(請注意我的示例ID#與名稱不匹配,請原諒我)
您可以先建立一個查詢字典,然后使用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.