-1

I´m generating a sales report since 2018 so I have two dataframes one dataframe gives the information of all the products, where It was sold, how many units and all of other type of information about the product and the other one gives the list of prices, one thing two take in consideration is that every store have a unique price for each product so I have something like these

##Dataframe 1

office codeprod units
001 12345 2
001 123456 3
002 123456 4
002 12345 5

##Dataframe 2

office codeprod price
001 12345 3.3
001 123456 4.5
002 123456 5.0
002 12345 3

What I need to do, its with both conditions office and codeprod, create a new column in my first dataframe with the price, I have tried np.where but I can´t get It done.

Psidom
  • 209,562
  • 33
  • 339
  • 356
PaulCoder
  • 27
  • 7
  • 1
    You need [merge](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.merge.html). – Psidom Feb 18 '22 at 00:01

1 Answers1

1

As suggested by @Psidom, use merge on two columns as follows:

df1.merge(df2, on=['office', 'codeprod'])  

Which for your two sample dataframes yields:

office  codeprod    units   price
0   001 12345        2      3.3
1   001 123456       3      4.5
2   002 123456       4      5.0
3   002 12345        5      3.0
itprorh66
  • 3,110
  • 4
  • 9
  • 21