如何将一个数据框映射到另一个(python pandas )?

编程入门 行业动态 更新时间:2024-10-08 22:59:36
本文介绍了如何将一个数据框映射到另一个(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).

谢谢!

推荐答案

使用掩码:

df1.set_index('ID', inplace=True) df2.set_index('ID', inplace=True) df1.mask(df1=='x',df2['score_x'],axis=0).mask(df1=='y',df2['score_y'],axis=0)

结果:

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 )?

本文发布于:2023-11-25 21:06:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631346.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   数据   pandas   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!