XYZ
XYZ

Reputation: 27387

How to merge multiple DataFrames and accumulate values for duplicated keys?

The request is to merge multiple DataFrames with duplicated "key" and accumulate the value associated with the key to a "master" DataFrame.

Is there a Pandas way to achieve the goal? Beside iterate every row and find the value based on the key OR using df.duplicated('ID', keep = False to find all duplicated keys and then adding the values to the "master" DataFrame.

My attempt was

pd.concat(dfs).duplicated('ID', keep = False)

enter image description here

Upvotes: 2

Views: 130

Answers (1)

jezrael
jezrael

Reputation: 862481

Use concat with aggregate sum:

df = pd.concat([df1, df2]).groupby('ID', as_index=False)['Value'].sum()

Upvotes: 5

Related Questions