用另一个数据框的值替换一个数据框的列值 [英] replace column values in one dataframe by values of another dataframe

查看:71
本文介绍了用另一个数据框的值替换一个数据框的列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据框,第一个有1000行,看起来像:

I have two dataframes , the first one has 1000 rows and looks like:

Date            Group         Family       Bonus
2011-06-09      tri23_1       Laavin       456
2011-07-09      hsgç_T2       Grendy       679
2011-09-10      bbbj-1Y_jn    Fantol       431
2011-11-02      hsgç_T2       Gondow       569

Group列具有不同的值,有时会重复,但通常大约有50个唯一值.

The column Group has different values, sometimes repeated, but in general about 50 unique values.

第二个数据框包含所有这50个唯一值(50行)以及与这些值关联的酒店:

The second dataframe contains all these 50 unique values (50 rows) and also the hotels, that are associated to these values:

Group             Hotel
tri23_1           Jamel
hsgç_T2           Frank
bbbj-1Y_jn        Luxy
mlkl_781          Grand Hotel
vchs_94           Vancouver

我的目标是用第二个数据帧的列Hotel的对应值替换第一个数据帧的列Group中的值,或者用相应的值创建列Hotel.当我尝试通过

My goal is to replace the value in the column Group of the first dataframe by the the corresponding values of the column Hotel of the second dataframe/or create the column Hotel with the corresponding values. When I try to make it just by assignment like

df1.loc[(df1.Group=df2.Group), 'Hotel']=df2.Hotel

我有一个错误,即数据帧的大小不相等,因此无法进行比较

I have an error that the dataframes are not of equal size, so the comparison is not possible

推荐答案

如果将索引设置为另一个df上的组"列,则可以使用

If you set the index to the 'Group' column on the other df then you can replace using map on your original df 'Group' column:

In [36]:
df['Group'] = df['Group'].map(df1.set_index('Group')['Hotel'])
df

Out[36]:
         Date  Group  Family  Bonus
0  2011-06-09  Jamel  Laavin    456
1  2011-07-09  Frank  Grendy    679
2  2011-09-10   Luxy  Fantol    431
3  2011-11-02  Frank  Gondow    569

这篇关于用另一个数据框的值替换一个数据框的列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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