0

I have two data frames df1 and df2. Both have first column common SKUCode=SKU

df1:

enter image description here

df2:

enter image description here

I want to update df1 and set SKUStatus=0 if SKUCode matches SKU in df2.

I want to add new row to df1 if SKU from df2 has no match to SKUCode.

So after the operation df1 looks like following:

enter image description here

One way I could get this done is via df2.iterrows() and looping through values however I think there must be another neat way of doing this? Thank you

import pandas as pdx

df1=pdx.DataFrame({'SKUCode':['A','B','C','D'],'ListPrice':[1798,2997,1798,999],'SalePrice':[1798,2997,1798,999],'SKUStatus':[1,1,1,0],'CostPrice':[500,773,525,300]})

df2=pdx.DataFrame({'SKUCode':['X','Y','B'],'Status':[0,0,0],'e_date':['31-05-2020','01-06-2020','01-06-2020']})


df1.merge(df2,left_on='SKUCode')
failsafe100
  • 123
  • 7

3 Answers3

0

I'm not sure exactly if I understood you correctly but I think you can use .loc. something along the lines of:

df1.loc[df2['SKUStatu'] != 0, 'SKUStatus'] = 1
snatchysquid
  • 1,283
  • 9
  • 24
0

You should have a look at pd.merge function [https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.merge.html].

First rename a column with the same name (e.g rename SKU to SKUCode). Then try:

df1.merge(df2, left_on='SKUCode')

If you provide input data (not screenshots), I can try with the appropriate parameters.

pyOliv
  • 1,253
  • 1
  • 6
  • 21
0

try this, using outer merge which gives both matching and non-matching records.

In [75]: df_m = df1.merge(df2, on="SKUCode", how='outer')                                                                                                         

In [76]: mask = df_m['Status'].isnull()                                                                                                                       

In [77]: df_m.loc[~mask, 'SKUStatus'] = df_m.loc[~mask, 'Status']

In [78]: df_m[['SKUCode', "ListPrice", "SalePrice", "SKUStatus", "CostPrice"]].fillna(0.0)

output

  SKUCode  ListPrice  SalePrice  SKUStatus  CostPrice
0       A     1798.0     1798.0        1.0      500.0
1       B     2997.0     2997.0        0.0      773.0
2       C     1798.0     1798.0        1.0      525.0
3       D      999.0      999.0        0.0      300.0
4       X        0.0        0.0        0.0        0.0
5       Y        0.0        0.0        0.0        0.0
sushanth
  • 8,275
  • 3
  • 17
  • 28