pythonpandasdataframe

Merge dataframes with repeated IDs


I have 2 dataframes, dfA & dfB. dfA contains purchases of certain products & dfB contains info on said products.

For instance, dfA:

purchaseID productID quantity
1 432 1
2 432 4
3 567 7

and dfB:

productID name
432 'mower'
567 'cat'

I wish to merge the two datasets on productID to produce something like:

purchaseID productID quantity name
1 432 1 'mower'
2 432 4 'mower'
3 567 7 'cat'

In actual fact, dfA & dfB are much larger.

How can I do this?

I understand how to do normal one-one merges, but struggling to see how to do one-many.


Solution

  • pd.merge(dfA, dfB, on='productID', how='inner')

    you can use merge along with the how type