0

I have a df1 which looks like this

  Company  SKU  Sales
1    A      X1   10
2    B      Y1   20
3    C      X1   30
4    D      Y1   40
5    E      X2   50
6    F      Y2   60

I have another df2 which only the list of SKU

    SKU  
1    X1  
2    Y1  

I want to merge these 2 dfs into another dataframe and the resulting dataframe should have the SKUs in the df2

  Company  SKU  Sales
1    A      X1   10
2    B      Y1   20
3    C      X1   30
4    D      Y1   40

I tried Left join but it doesn't seem to be working

df3 = df1.merge(df2,how='Left',on='SKU')

I will appreciate your kind help.

Ahamed Moosa
  • 1,395
  • 7
  • 16
  • 30

1 Answers1

1

I looks like you aren't trying to 'join' the dfs, but rather to select rows from the first where the SKUs match those in the second.

Try something like this:

df1.loc[df1['SKU'].isin(df2.SKU)]

See: Select rows from a DataFrame based on values in a column in pandas

jpp
  • 159,742
  • 34
  • 281
  • 339
Neil
  • 3,020
  • 4
  • 25
  • 48