Skip to content
Advertisement

Combine dataframes based on multiple conditions in python

Table A

JavaScript

Table B

JavaScript

Table C

JavaScript

These are example tables that represent dataframes that I’m going to create from seperate excel sheets.

Basically there’s a many to many relationship going on and I want to be able to create a combined sheet that will roll up the “amount” total (from Table A) for each year of the item (Table B). I don’t want to duplicate the amounts for multiple items with the same year as you see in table A, item A has two 2011 entries.

What I’d like to get is a result like Table C below

I’m currently trying to use pandas merge but not having much luck. Can anyone lend a hand?

Advertisement

Answer

IIUC, you can use a merge and post-process to remove the duplicates per year:

JavaScript

output:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement