2

Here is the code:

input:

df1 = pd.DataFrame({'a': [1,2,3], 'b': [2,3,4], 'c':['aa','bb','cc'], 'd':[5,9,1]})

df2 = pd.DataFrame({'a': [4,5,6], 'b': [7,8,9], 'c':['dd','ee','ff'], 'd':[50,90,10]})


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

output:

   a   b     c   d
0  5   9  aadd  55
1  7  11  bbee  99
2  9  13  ccff  11

But I want my output to like this:

   a   b     c   d
0  1   9  aadd  55
1  2  11  bbee  99
2  3  13  ccff  11

'a' column should not be added, it should remain the same as in df1.

How can I achieve that?

Thanks in advance.

1 Answer 1

2

One idea is remove column a from second DataFrame:

combinedframes_add = df1.add(df2.drop('a', axis=1), fill_value=0)
print (combinedframes_add)
     a   b     c   d
0  1.0   9  aadd  55
1  2.0  11  bbee  99
2  3.0  13  ccff  11

Or set 0 to column a:

combinedframes_add = df1.add(df2.assign(a=0), fill_value=0)
print (combinedframes_add)
   a   b     c   d
0  1   9  aadd  55
1  2  11  bbee  99
2  3  13  ccff  11
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.