简体   繁体   English

Python:如何在比较其他列时将列值填充到另一个 dataframe 中的新列?

[英]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.

相关问题 如何通过将一个列与另一个数据框进行比较来填充列 - How to populate a column in one dataframe by comparing it to another dataframe 如何根据 Python 中的另一个数据框列填充列? - How to populate a column base on another dataframe column in Python? 仅当新值不为空时,如何从 dataframe 比较来自另一个 dataframe 的值来更改列值? - How to change a column values from dataframe comparing value from another dataframe only if the new value is not empty? 检查一个数据框的列名是否与另一个数据框的索引值匹配,并将值填充到新列中 - Check if column name from one dataframe matches with index value of another dataframe and populate value into a new column Python,比较 dataframe 行,添加新列 - 真值错误? - Python, comparing dataframe rows, adding new column - Truth Value Error? 如何根据另一列中的数据填充 dataframe 中的列并在 python 中的另一列上进行条件/切换 - How to populate a column in a dataframe based on data in another column and condition /switch on another column in python 根据另一个 dataframe 中列的 if 语句填充 dataframe 中的列 - Python - Populate a column in a dataframe based on if statement for column in another dataframe - Python 通过解析列值为数据框创建新列,并使用来自另一列python的值填充新列 - Create new columns for a dataframe by parsing column values and populate new columns with values from another column python 如何根据 python 中的另一列填充一列? - How to populate a column based another column in python? 根据其他 dataframe 的列填充一个 dataframe 列 - Populate a dataframe column based on a column of other dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM