使用不同的索引在Python中添加两个数据pandas系列

时间:2015-10-29 17:32:20

标签: python pandas indexing

我有两个数据系列

A

0 1

1 2

2 3

B

3 1

4 2

5 3

从我想要的地方

C = A + B

C

0 2

1 4

2 6

我尝试了A + B.reindex_like(A),但它没有用。我尝试过的所有东西都给了我NaN's。有什么帮助吗?

1 个答案:

答案 0 :(得分:2)

你可以使用reset_index(drop=True),因为A的索引是默认的吗?

In [127]: A + B.reset_index(drop=True)
Out[127]:
0    2
1    4
2    6
dtype: int64

或者,

In [128]: pd.Series(A.values + B.values, index=A.index)
Out[128]:
0    2
1    4
2    6
dtype: int64