Home > Net >  Join differently sized dataframes on column
Join differently sized dataframes on column

Time:03-15

I have a dataframe of item sales, and a smaller one of costs for each item, called june_2008 and costs_2008. I need to join them so that each of many sales has its price nex to it, so I can get things like profit margins.

enter image description here

enter image description here

The intent is to match them on the SKU index. I've tried with/without the index.

I've been trying merge and join commands in pandas, but don't know the specific details and can't get them joined as needed. Can you show how to do this?

Thank you.

CodePudding user response:

First of all, the "SKU" is an index, I recommend you create a column called "SKU" using reset_index command, like this:

june_2008.reset_index(inplace=True)
costs_2008.reset_index(inplace=True)

After that, a simple merge should resolve the problem:

pd.merge(june_2008, costs_2008, on=["SKU"])

CodePudding user response:

My issue was that the "SKU" columns were different datatypes. Once they're made identical a merge command is simple.

  • Related