Alien13
Alien13

Reputation: 618

Add two columns from two different dataframes, with all other columns being equal

How do I add two columns from two different DataFrames, where all columns are equal, except those I want to add?

See the example code below:

#IN:
import pandas as pd
import numpy as np
#Observe that 'col2' in both dataframes are equal
df1 = pd.DataFrame({'col1': [3.2, 4.4, 6.5, np.NaN], 'col2': [1.2, 2.2, np.NaN, 5.7]})

     col1    col2
0    3.2     1.2
1    4.4     2.2
2    6.5     nan
3    nan     5.7

df2 = pd.DataFrame({'col1': [1.0, 2.0, 3.0, 4.0], 'col2': [1.2, 2.2, np.NaN, 5.7]})

     col1    col2
0    1.0     1.2
1    2.0     2.2
2    3.0     nan
3    4.0     5.7

# new_df = [INSERT SOME MAGIC FUNCTION HERE]
# I tried: new_df = df1.add(df2, level='col1')    
# but obviously that did not work

#OUT (This is what I want):
print(new_df)

     col1   col2
0    4.2    1.2
1    6.4    2.2
2    9.5    nan
3    4.0    5.7

Observe that np.NaN + some_float = some_float

Upvotes: 2

Views: 66

Answers (2)

YOLO
YOLO

Reputation: 21739

I think you can simply do:

new_df = df1.copy()
new_df['col1'] = df1['col1'].fillna(0).add(df2['col1'])

    col1    col2
0   4.2     1.2
1   6.4     2.2
2   9.5     NaN
3   4.0     5.7

Upvotes: 1

jpp
jpp

Reputation: 164773

This is one way. Just remember to fillna(0) when you add 2 series.

res = pd.DataFrame({'col1': df1['col1'].fillna(0) + df2['col1'].fillna(0),
                    'col2': df1['col2']})

print(res)

#    col1  col2
# 0   4.2   1.2
# 1   6.4   2.2
# 2   9.5   NaN
# 3   4.0   5.7

Upvotes: 3

Related Questions