Home > Back-end >  Python Pandas - Find and replace across two dataframes
Python Pandas - Find and replace across two dataframes

Time:06-01

I searched a lot but I can't solve my problem - maybe someone can give me a hint:

I have two Panda Dataframes, df1 and df2, with these columns:

EAN PRODUCT-NAME OLD-DESCRIPTION PURCHASE-PRICE SALES-PRICE

EAN NEW-PRODUCT-NAME NEW-DESCRIPTION

I want to locate a product in df1 by the EAN number, and replace it's name and description with the new name & description in the corresponding product in df2. It is important to know, that the first dataset is bigger and not every product will get a new name and description. I can't just replace the whole columns.

I have to go take the EAN numbers in df2, search for them in df1 to find the right product, and replace the name and description in df2.

I tried various methods like replace, isin, and even thought about nesting two loopsto solve the problem. But I might think to complicated...

CodePudding user response:

EAN column is not common between both tables. df1 is way bigger.

I thought about something like this, but this is not elegant at all...

for x in df1.index:
   for y in df1.index:
      if df1.loc[x, "EAN"] == df2.loc[y, "EAN"]:
         print ("Found!") 
            now replace...

CodePudding user response:

Assumption : EAN column is common between 2 table

df3=pd.merge(df1,df2 , on= 'EAN',how= 'Outer')

further you can filter colums which you need and also rename it as needed.

  • Related