鉴于这两个数据帧,我如何获得预期的输出数据帧?长的方法是使用 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 ydf2:
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 )?
发布评论