简体   繁体   中英

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM