简体   繁体   English

如何通过将df ID#替换为另一个df的名称来创建新的df?

[英]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.

相关问题 如何通过有条件地查找另一个 DF 并将结果水平附加到新的 DF 中来创建 DF? - How can I create a DF by conditionally looking up into another DF, and appending the results horizontally into a new DF? 如何创建一个新的DF,其中最后一列乘以3/2过滤另一个DF的ID#? - How to create a new DF where the last column is multiplied by 3/2 filtering another DF's ID #? 如何使用for循环和从其他df派生的新列名重命名df的列? - How do I rename columns of a df with a for loop and new column names deriving from other df? 如何用df1替换df列名 - How to replace df column names with df1's 如何根据条件从一个原始 df 创建多个 df,然后为它们分配单独的名称 - How do I create several df's out of one original df based on a condition and then assign them individual names 如何将列名从一个 df 索引到另一个 df 系列? - How to index column names from one df to another df series? 我怎样才能最好地使用来自另一个用于检索多个值的 df 的索引值创建一个新的 df? - How can I best create a new df using index values from another df that are used to retrieve multiple values? 我如何将分组的屏蔽 id 除以另一个 df 中的值(df 包含每个屏蔽 id 的值) - how can i divide grouped masked id by a value that is in another df (the df contains a value for each masked id ) 循环 df 并创建新的 df - Loop through df and create new df 熊猫创建的df是另一个df中列的乘积 - Pandas create df that is the product of columns in another df
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM