如何将一个数据框映射到另一个(python pandas )? [英] How to map one dataframe to another (python pandas)?

查看:59
本文介绍了如何将一个数据框映射到另一个(python pandas )?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于这两个数据帧,我如何获得预期的输出数据帧?长的方法是使用 iloc 遍历数据帧的行,然后在将 df2 转换为之后使用 map 函数dict 将x和y映射到它们的得分.

Given these two dataframes, how do I get the intended output dataframe? The long way would be to loop through the rows of the dataframe with iloc and then use the map function after converting df2 to a dict to map the x and y to their score.

这似乎很乏味,并且在大型数据帧上运行将花费很长时间.我希望有一个更清洁的解决方案.

This seems tedious and would take long to run on a large dataframe. I'm hoping there's a cleaner solution.

df1:

ID    A    B    C
1     x    x    y
2     y    x    y
3     x    y    y

df2:

ID    score_x    score_y
1          20         30
2          15         17
3          18         22

输出:

ID    A     B     C
1     20    20    30
2     17    15    17
3     18    22    22

请注意:数据框将有很多列,并且类别将不止x和y(可能在20个类别的范围内).

Note: the dataframes would have many columns and there would be more than just x and y as categories (possibly in the region of 20 categories).

谢谢!

推荐答案

使用结果:

     A   B   C
ID            
1   20  20  30
2   17  15  17
3   18  22  22

如果有很多列,并且它们都以相同的方式命名,则可以使用类似的内容:

If there are many columns and they are all named in the same way, you can use something like that:

for e in df2.columns.str.split('_').str[-1]:
     df1.mask(df1==e, df2['score_'+e], axis=0, inplace=True)

这篇关于如何将一个数据框映射到另一个(python pandas )?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆