NaN 的总和等于 NaN(非零)

Sum of NaNs to equal NaN (not zero)

我可以使用 df['TOTAL'] = df.sum(axis=1) 向此 DF 添加一个 TOTAL 列,它会像这样添加行元素:

   col1  col2  TOTAL
0   1.0   5.0    6.0
1   2.0   6.0    8.0
2   0.0   NaN    0.0
3   NaN   NaN    0.0

但是,我希望底行的总和为 NaN,而不是零,如下所示:

   col1  col2  TOTAL
0   1.0   5.0    6.0
1   2.0   6.0    8.0
2   0.0   NaN    0.0
3   NaN   NaN    Nan

有没有一种方法可以高效地实现这一点?

将参数min_count=1添加到DataFrame.sum:

min_count : int, default 0
The required number of valid values to perform the operation. If fewer than min_count non-NA values are present the result will be NA.

New in version 0.22.0: Added with the default being 0. This means the sum of an all-NA or empty Series is 0, and the product of an all-NA or empty Series is 1.

df['TOTAL'] = df.sum(axis=1, min_count=1)
print (df)
   col1  col2  TOTAL
0   1.0   5.0    6.0
1   2.0   6.0    8.0
2   0.0   NaN    0.0
3   NaN   NaN    NaN