As title, how can I add up pandas dfs that contains some nan value while retaining all values in those entries(i.e. 0+nan=0)?
DF1=
A1 | B1 | C1 | D1 |
---|---|---|---|
0 | 0 | nan | nan |
0 | 1 | 1 | nan |
DF2=
A2 | B2 | C2 | D2 |
---|---|---|---|
nan | nan | nan | nan |
nan | 1 | 1 | 1 |
Expected output=
A sum | B sum | C sum | D sum |
---|---|---|---|
0 | 0 | nan | nan |
0 | 2 | 2 | 1 |
Sorry if the question is too basic, thanks for answering.
You need to use add
with fill_value=0
and to avoid index alignment by covering to_numpy
:
df1.add(df2.to_numpy(), fill_value=0)
Output:
A1 B1 C1 D1
0 0.0 0.0 NaN NaN
1 0.0 2.0 2.0 1.0
With column names:
(df1.add(df2.to_numpy(), fill_value=0)
.rename(columns=lambda c: c[:-1] + ' sum')
)
Output:
A sum B sum C sum D sum
0 0.0 0.0 NaN NaN
1 0.0 2.0 2.0 1.0