[英]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. 我正在尝试在pandas数据框中创建一个新列,该列对其他列的总和进行求和。 However, if any of the source columns are blank (NaN or 0), I need the new column to also be written as blank (NaN)
但是,如果任何源列为空(NaN或0),我需要将新列写为空白(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 我目前正在使用pd.sum函数,格式如下
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. 它会忽略NaN,但不会将NaN写入sum列。
Thanks in advance for any advice 提前感谢任何建议
replace
your 0 to np.nan
then pass skipna = False
replace
你的0 replace
为np.nan
然后传递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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.