简体   繁体   English

PANDAS - 如何根据另一个 dataframe 中的查找表替换 dataframe 中的值

[英]PANDAS - How do I replace the values in a dataframe based on a lookup table in another dataframe

I have two csv files:我有两个 csv 文件:

live_file.csv live_file.csv

Supplier SKU, Manufacturer SKU, Price
ABCD, 900000, 10
EFGH, 800000, 10

old_file.csv old_file.csv

Supplier SKU, Manufacturer SKU, Price
ABCD, 91234, 10
EFGHX, 85332, 10

I want to find the same values in the column Supplier SKU column, when I find matching vaues I want to take the Manufacturer SKU value from old_file.csv and put it in the live_file.csv, so my result will be:我想在供应商 SKU 列中找到相同的值,当我找到匹配的值时,我想从 old_file.csv 中获取制造商 SKU 值并将其放入 live_file.csv 中,所以我的结果将是:

Supplier SKU, Manufacturer SKU, Price
ABCD, 91234, 10
EFGH, 800000, 10

This is what i tried:这是我尝试过的:

import pandas as pd

live_file  = pd.read_csv("live.csv")
old_file   = pd.read_csv("old.csv") 

old_file = old_file.set_index('Supplier SKU')['Manufacturer SKU'].dropna()
live_file['Manufacturer SKU'] = live_file['Supplier SKU'].replace(old_file)

live_file.to_csv(r'final.csv')

But this does not work, the end file is the same as the live file at the beginning, any help?但这不起作用,结束文件与开始时的实时文件相同,有什么帮助吗?

Set index on 'Supplier SKU' using set_index , then call update on new livefile.使用set_index在“供应商 SKU”上设置索引,然后在新的 livefile 上调用update

import pandas as pd

df1 = pd.DataFrame({
    'Supplier SKU': ['ABCD','EFGH'],
    'Manufacturer SKU': [900000, 800000],
    'Price': [10, 10]
}).set_index('Supplier SKU')
df2 = pd.DataFrame({
    'Supplier SKU': ['ABCD','EFGHX'],
    'Manufacturer SKU': [91234, 85332],
    'Price': [10, 10]
}).set_index('Supplier SKU')

df1.update(df2)
print(df1)

result:结果:

在此处输入图像描述

To prevent 'Price' also being updated, you can just drop Price in df2:为了防止“价格”也被更新,您可以在 df2 中删除价格:

df1.update(df2.drop(columns='Price'))

PS: call df1.reset_index() to make 'Supplier SKU' into ordinary column PS:调用df1.reset_index()将 'Supplier SKU' 变成普通列

You can basically do a left-merge join of the two files on the column Supplier SKU and then keep the value of column Manufacturer SKU from old_file when the merge matches, otherwise keep the value from live_file您基本上可以对列Supplier SKU上的两个文件进行左合并连接,然后在合并匹配时保留列Manufacturer SKU的值来自old_file ,否则保留来自live_file的值

live_file["Manufacturer SKU"] =  pd.merge(live_file[["Supplier SKU", "Manufacturer SKU"]], 
         old_file[["Supplier SKU", "Manufacturer SKU"]],
         how="left", 
         on="Supplier SKU",
         suffixes=(None, "__right"),
         indicator="merge_flag")\
         .apply(lambda row: row["Manufacturer SKU"] 
                if row["merge_flag"] == "left_only" 
                else row["Manufacturer SKU__right"], axis=1) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据另一个数据框中的查找表替换数据框中的值 - How do I replace the values in a dataframe based on a lookup table in another dataframe 如何根据另一个 dataframe 中的查找值替换 pandas dataframe 值? - How to replace pandas dataframe values based on lookup values in another dataframe? 使用另一个 Dataframe 作为查找表替换 Pandas Dataframe 中的值 - Replace values in Pandas Dataframe using another Dataframe as a lookup table 通过从另一个数据帧中查找来替换 Pandas 数据帧中的所有值 - Replace all values in pandas dataframe by lookup from another dataframe 如何在熊猫中通过ID将数据框的列值替换为另一个数据框值 - How do I replace a dataframe's columns values with another dataframe values by Id in Pandas 如何用另一个 dataframe 替换 pandas dataframe 中的值 - How to replace values in pandas dataframe with another dataframe 如何根据公共列将 dataframe 的列值替换为另一个 dataframe 的值? - How do I replace column values of a dataframe with values of another dataframe based on a common column? 如何根据另一个表的检查替换 pandas dataframe 中的列中的值 - How to replace the values in a column in pandas dataframe based on check from another table 使用Series查找表替换Pandas DataFrame列中的值 - Replace values in column of Pandas DataFrame using a Series lookup table 如何根据条件替换熊猫数据框中的值? - How to replace values in a pandas dataframe based on conditions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM