0

I have 3 dataframes where I have one string column which I want to merge on and 2 similar columns which I want to add up

df1:

index user x y
 1    john 5 6
 2    pete 10 10 

df2:

index user x y
 1    john 1 1
 2    pete 2 2 
 3    nash 5 5

df3:

index user x y
 1    nash 6 7
 2    john 2 4 
 3    kyle 3 3

I want: df4:

index user x y
 1    john 8 11
 2    pete 12 12 
 3    nash 11 12
 4    kyle 3  3
salanimi
  • 63
  • 1
  • 8
  • Does this answer your question? [pandas three-way joining multiple dataframes on columns](https://stackoverflow.com/questions/23668427/pandas-three-way-joining-multiple-dataframes-on-columns) –  Aug 21 '20 at 10:40

1 Answers1

3

try this, first pandas.concat then groupby

import pandas as pd

pd.concat([df1, df2, df3]).groupby(["user"], as_index=False)[['x', 'y']].sum()

   user   x   y
0  john   8  11
1  kyle   3   3
2  nash  11  12
3  pete  12  12
sushanth
  • 8,275
  • 3
  • 17
  • 28