0

I want to copy the information (quantity) of one dataframe's column to the other dataframe's quantity column but do so matching the SKU column.

So for example the dataframes look like:

Dataframe 1: 
SKU    Quantity      Title
A          3         Scissors
B          4         Cable
C          5         Goat
D          6         Cheese
Dataframe 2: 
SKU    Quantity      Title
A          1         Blue Scissors
B          2         Red Cables
C          1         Fat Goat
D          2         Smelly Cheese

So I would like to get Dataframe 1's quantities and place them into Dataframe 2, but matching the SKUs (A, B, C, D etc) even though some other columns (such as Title) might have different information.

Stevenson
  • 83
  • 1
  • 10

2 Answers2

1

You can try to set index on SKU for both dataframes to align on index and copy the column Quantity with the aligned index. Reset index to restore SKU back to data column.

df1a = df1.set_index('SKU')
df2a = df2.set_index('SKU')

df2a['Quantity'] = df1a['Quantity']

df2 = df2a.reset_index()

Result:

print(df2)

  SKU  Quantity          Title
0   A         3  Blue Scissors
1   B         4     Red Cables
2   C         5       Fat Goat
3   D         6  Smelly Cheese
SeaBean
  • 22,547
  • 3
  • 13
  • 25
0

You could try this :

df2['Quantity'] = np.where(df1['SKU'] == df2['SKU'], df1['Quantity'])
heretolearn
  • 6,387
  • 4
  • 30
  • 53