添加两个pandas Series,同时在非匹配索引处保留(非数字)值

时间:2014-05-14 14:50:21

标签: python pandas

假设我有以下两个pandas.Series个对象:

>>> s1 = Series([1, True, 3, 5], index=['a', 'b', 'c', 'e'])
>>> s2 = Series([100, 300, 'foo', 500], index=['a', 'c', 'd', 'e'])
>>> s1
a       1
b    True
c       3
e       5
dtype: object
>>> s2
a    100
c    300
d    foo
e    500
dtype: object

我想计算总和s1+s2,其中非匹配索引处的元素应该只是从s1s2“继承”而不做任何更改。所以结果应该是这样的:

>>> Series([101, True, 303, 'foo', 505], index=['a', 'b', 'c', 'd', 'e'])
a     101
b    True
c     303
d     foo
e     505
dtype: object

天真的方法不起作用,因为它在不匹配的索引处引入了NaN

>>> s1 + s2
a    101
b    NaN
c    303
d    NaN
e    505
dtype: object

StackOverflow上有类似的问题提出了s1.add(s2, fill_value=0)的解决方案,但这在我的案例中不起作用,因为并非所有值都是数字,所以fill_value没有用。有没有一个选项告诉大熊猫只是填写他们所在的系列中的缺失值?

这似乎是一件显而易见的事情,我肯定会遗漏一些东西,但我一直在搜索文档和StackOverflow并且没有找到任何答案。非常感谢任何建议!

1 个答案:

答案 0 :(得分:3)

我认为使用combine_first的一种方式是:

>>> s1 = pd.Series([1, True, 3, 5], index=['a', 'b', 'c', 'e'])
>>> s2 = pd.Series([100, 300, 'foo', 500], index=['a', 'c', 'd', 'e'])
>>> (s1+s2).combine_first(s1).combine_first(s2)
a     101
b    True
c     303
d     foo
e     505
dtype: object

但说实话,Series这种类型不同似乎不常出现在我身上。

相关问题