Python Pandas 对两列求和,跳过 NaN

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24386638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:31:55  来源:igfitidea点击:

Pandas sum two columns, skipping NaN

pythonpandas

提问by smontanaro

If I add two columns to create a third, any columns containing NaN (representing missing data in my world) cause the resulting output column to be NaN as well. Is there a way to skip NaNs without explicitly setting the values to 0 (which would lose the notion that those values are "missing")?

如果我添加两列来创建第三列,任何包含 NaN(代表我的世界中缺失的数据)的列都会导致结果输出列也是 NaN。有没有办法跳过 NaN 而不将值明确设置为 0(这会失去这些值“缺失”的概念)?

In [42]: frame = pd.DataFrame({'a': [1, 2, np.nan], 'b': [3, np.nan, 4]})

In [44]: frame['c'] = frame['a'] + frame['b']

In [45]: frame
Out[45]: 
    a   b   c
0   1   3   4
1   2 NaN NaN
2 NaN   4 NaN

In the above, I would like column c to be [4, 2, 4].

在上面,我希望 c 列是 [4, 2, 4]。

Thanks...

谢谢...

采纳答案by jrjc

with fillna()

使用填充()

frame['c'] = frame.fillna(0)['a'] + frame.fillna(0)['b']

or as suggested :

或按照建议:

frame['c'] = frame.a.fillna(0) + frame.b.fillna(0)

giving :

给予:

    a   b  c
0   1   3  4
1   2 NaN  2
2 NaN   4  4

回答by DSM

Another approach:

另一种方法:

>>> frame["c"] = frame[["a", "b"]].sum(axis=1)
>>> frame
    a   b  c
0   1   3  4
1   2 NaN  2
2 NaN   4  4