Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I have 2 data frames

df1

    Name    2010  2011

0   Jack     25    35

1   Jill     15    20

df2

    Name    2010  2011

0   Berry    45    25

1   Jack     5     10

I want to create the third data frame by adding the values in these data frames

Desired Output

df3

    Name    2010  2011

0   Jack     30    45      #add the values from df1 and df2

1   Jill     15    20

2   Berry    45    25

I have used the code 

df1.add(df2)

1 Answer

0 votes
by (36.8k points)
edited by

Concat both dfs and do a groupby and sum:

print (pd.concat([df, df2]).groupby("Name", as_index=False).sum())

    Name  2010  2011

0  Berry    45    25

1   Jack    30    45

2   Jill    15    20

Do check out Data Science with Python course which helps you understand from scratch  

Browse Categories

...