gpt4 book ai didi

pandas - NaN 的总和等于 NaN(非零)

转载 作者:行者123 更新时间:2023-12-05 05:12:28 25 4
gpt4 key购买 nike

我可以使用 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

关于pandas - NaN 的总和等于 NaN(非零),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54567391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com