我有两个系列,series1
和series2
。
series1
的索引是series2.
我想基本上将这些新索引值添加到第一个系列(在series2中但不在series1中),并将它们设为0值。
答案 0 :(得分:3)
Pandas Index
对象有一些内置的方法来执行set操作。根据svenkatesh模拟的示例数据,我们可以获得s1
和s2
索引的并集,并提供填充值:
import pandas as pd
s1 = pd.Series(range(1, 10), index=range(0,9))
s2 = pd.Series(range(1, 4), index=range(0,3))
# `|` represents the union operation
# This is not an inplace operation by default, so
# you need to assign the result back to `s2` if
# you want to keep the changes
s2.reindex(s2.index | s1.index, fill_value=0)
Out[53]:
0 1
1 2
2 3
3 0
4 0
5 0
6 0
7 0
8 0
dtype: int64
答案 1 :(得分:1)
使用您在评论中提供的示例,我构建了两个示例系列,s1
和s2
,其中每个系列都具有相同的公共索引值。对于不在s1
但在s2
中的PRESENT的索引值,我将s1
中的值指定为0.
In [1]: import pandas as pd
In [2]: s1 = pd.Series(range(1, 10), index=range(0,9))
In [3]: s1
Out[3]:
0 1
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
dtype: int64
In [4]: s2 = pd.Series(range(1, 4), index=range(0,3))
In [5]: s2
Out[5]:
0 1
1 2
2 3
dtype: int64
In [6]: join = ~((s1.index.isin(s2.index)))
In [7]: join
Out [7]: array([False, False, False, True, True, True, True, True, True], dtype=bool)
In [8]: s1.loc[join] = 0
In [9]: s1
Out [9]:
0 1
1 2
2 3
3 0
4 0
5 0
6 0
7 0
8 0
dtype: int64