如何将一个系列分配给另一个系列或在索引不相交的地方合并并保留其中一个系列的值以用于有趣的索引
How do you assign one series to another or merge where index does not intersect and retain values from one of the series for the interesting indices
我有
S1 = pd.Series([1,2,3,4], index = ['A', 'B', 'C', 'D'])
S2 = pd.Series([5,6,7,8], index = ['C', 'D', 'E', 'F'])
我想得到
S3 = pd.Series([1,2,5,6,7,8], index = ['A', 'B', 'C', 'D', 'E', 'F'])
如果索引在 S1
但不在 S2
中,则将值保留在 S1
中。如果索引不在 S1
但在 S2
中,则将值保留在 S2
中。如果 S1
和 S2
中的索引保持 S2
.
中的值
它也可以看作是S1
和S2
的合并,但是对于索引感兴趣的值保持S2
中的值。
如果所有输出值都是整数,我认为你需要combine_first
with astype
:
S3 = S2.combine_first(S1).astype(int)
print (S3)
A 1
B 2
C 5
D 6
E 7
F 8
dtype: int32
我有
S1 = pd.Series([1,2,3,4], index = ['A', 'B', 'C', 'D'])
S2 = pd.Series([5,6,7,8], index = ['C', 'D', 'E', 'F'])
我想得到
S3 = pd.Series([1,2,5,6,7,8], index = ['A', 'B', 'C', 'D', 'E', 'F'])
如果索引在 S1
但不在 S2
中,则将值保留在 S1
中。如果索引不在 S1
但在 S2
中,则将值保留在 S2
中。如果 S1
和 S2
中的索引保持 S2
.
它也可以看作是S1
和S2
的合并,但是对于索引感兴趣的值保持S2
中的值。
如果所有输出值都是整数,我认为你需要combine_first
with astype
:
S3 = S2.combine_first(S1).astype(int)
print (S3)
A 1
B 2
C 5
D 6
E 7
F 8
dtype: int32