0

I have two data frames. I need to join them so that those index which have same name in both data frames are joined into one and their values summed. Those index which do not exist in the other data frame are created and their valued inserted. See example below.

dataFrame1:

index   col1 col2 col3
A         3    0    4
C         4    1    2
D         3    5    6
G         3    0    0

dataFrame2

index   col1 col2 col3
A         1    1    3
B         4    4    1
C         1    3    0
E         0    2    1
F         1    3    2

I need the following results:

index   col1 col2 col3
A         4    1    7
B         4    4    1
C         5    4    2
D         3    5    6
E         0    2    1
F         1    3    2
G         3    0    0

how can I do this is pandas? Note: no value should be treated as zero unless it is zero or NaN in both data frames.

1

1 Answer 1

4

I think you can use add with combine_first and casting to int by astype:

print df1
       col1  col2  col3
index                  
A         3     0     4
C         4     1     2
D         3     5     6
G         3     0     0

print df2
       col1  col2  col3
index                  
A         1     1     3
B         4     4     1
C         1     3     0
E         0     2     1
F         1     3     2

print df1.add(df2).combine_first(df1).combine_first(df2).astype(int)
       col1  col2  col3
index                  
A         4     1     7
B         4     4     1
C         5     4     2
D         3     5     6
E         0     2     1
F         1     3     2
G         3     0     0
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.