MJK
MJK

Reputation: 55

Pandas: Sum multiple columns, but write NaN if any column in that row is NaN or 0

I am trying to create a new column in a pandas dataframe that sums the total of other columns. However, if any of the source columns are blank (NaN or 0), I need the new column to also be written as blank (NaN)

a    b    c    d    sum

3    5    7    4    19
2    6    0    2    NaN    (note the 0 in column c)
4    NaN  3    7    NaN

I am currently using the pd.sum function, formatted like this

 df['sum'] = df[['a','b','c','d']].sum(axis=1, numeric_only=True)

which ignores the NaNs, but does not write NaN to the sum column.

Thanks in advance for any advice

Upvotes: 4

Views: 991

Answers (1)

BENY
BENY

Reputation: 323346

replace your 0 to np.nan then pass skipna = False

df.replace(0,np.nan).sum(1,skipna=False)
0    19.0
1     NaN
2     NaN
dtype: float64
df['sum'] = df.replace(0,np.nan).sum(1,skipna=False)

Upvotes: 1

Related Questions