Guy Barash
Guy Barash

Reputation: 490

sum numbers in two dataframes based on their intersecting indexes

i have 2 dataframes with some common index and some that are not:

df1
   DATA
1     1
2     2
3     3
4     4
5     5

df2
   DATA
3     3
4     4
5     5
6     6
7     7

I want to sum/take max (i actually need both for different cols) them, and consider missing indexes as 0. In this example the result should be:

df_results
   DATA
1     1
2     2
3     6
4     8
5    10
6     6
7     7

where 3,4,5 were summed, but the rest remained the same.

Thx!

Upvotes: 0

Views: 64

Answers (1)

gtomer
gtomer

Reputation: 6574

Try this:

combined = df1.add(df2, fill_value=0)

Upvotes: 5

Related Questions