pandas add dataframes without NaN values in case of missing entries

Viewed 21

What is the most elegant way to add two pandas DataFrames without getting NaNs in case of one missing column/row.

It is probably better described with this simple example:

index1 = pd.Index(["a", "b"])
data1 = np.full(shape=(2, 2), fill_value=1.0)
df1 = pd.DataFrame(data1, index=index1, columns=index1)
# df1 = 
#    a   b  
# a 1.0 1.0 
# b 1.0 1.0 

index2 = pd.Index(["b", "c"])
data2 = np.full(shape=(2, 2), fill_value=2.0)
df2 = pd.DataFrame(data2, index=index2, columns=index2)
# df2 = 
#    b   c  
# b 2.0 2.0 
# c 2.0 2.0 

df = df1 + df2

The resulting dataframe is

   a   b   c 
a nan nan nan
b nan 3.0 nan
c nan nan nan

However, I would like it to be

   a   b   c 
a 1.0 1.0 nan
b 1.0 3.0 2.0
c nan 2.0 2.0
1 Answers

You can use add with fill_value=0:

df1.add(df2, fill_value=0)

output:

     a    b    c
a  1.0  1.0  NaN
b  1.0  3.0  2.0
c  NaN  2.0  2.0

documentation of the parameter:

fill_value: float or None, default None

Fill existing missing (NaN) values, and any new element needed for successful DataFrame alignment, with this value before computation. If data in both corresponding DataFrame locations is missing the result will be missing.

Related