Home > Mobile >  Accumulate values ​from one df into another
Accumulate values ​from one df into another

Time:12-01

I have two dataframes and I want to accumulate the value of one of the dataframes in the other. How can I do it?

Dataframe 1:

Product Amount Price Total
A 1 12.0 15
B 4 20.0 15
C 2 4.0 15
D 5 30.0 15

Dataframe 2:

Product Amount Price
B 3 20.0
C 2 4.0

Result:

Product Amount Price Total
A 1 12.0 15
B 7 20.0 15
C 4 4.0 15
D 5 30.0 15

Thanks!

CodePudding user response:

Use concat with aggregate sum - necessary all numeric columns without Product:

df = pd.concat([df1, df2]).groupby('Product', as_index=False).sum()
print (df)
  Product  Amount  Price  Total
0       A       1   12.0   15.0
1       B       7   40.0   15.0
2       C       4    8.0   15.0
3       D       5   30.0   15.0
  • Related