[英]Python : How to populate a column value to new column in another dataframe on comparing other column?
Have got two dataframe有两个 dataframe
df df
Store Sku Fixture
11 AA Product
12 BB Tier
df1 df1
Store Sku Bit
11 AA 1
11 AA 2
12 CC 1
12 CC 2
12 CC 3
So, comparing the 'Store' column from dataframes, need to populate 'Fixture' column in df1 from df.因此,比较数据帧中的“存储”列,需要从 df 填充 df1 中的“夹具”列。 Expected Output:
预期 Output:
Store Sku Bit Fixture
11 AA 1 Product
11 AA 2 Product
12 CC 1 Tier
12 CC 2 Tier
12 CC 3 Tier
Thanks in Advance!提前致谢!
You are using dataframes as a relational database.您正在使用数据框作为关系数据库。 This can be achieved using
Series.map
这可以使用
Series.map
来实现
First, build a dictionary holding the relation used for the mapping:首先,构建一个包含用于映射的关系的字典:
map_dict = df.set_index('Store')['Fixture'].to_dict()
set_index
uses a column as row labels. set_index
使用列作为行标签。 By default, it does not modify the original DataFrame
.默认情况下,它不会修改原来的
DataFrame
。
Then, we use this dict for a mapping of the 'Store' Series
, and append the output to df1
:然后,我们使用这个 dict 映射 'Store'
Series
和 append output 到df1
:
df1['Fixture'] = df1['Store'].map(map_dict)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.