Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding two data pandas series in Python with different index

I have two data series in form

A

0 1

1 2

2 3

B

3 1

4 2

5 3

From where I want

C = A + B

to be

C

0 2

1 4

2 6

I have tried A + B.reindex_like(A) but it doesn't work. Everything I've tried gives me NaN's. Any help?

like image 468
ONils Avatar asked Sep 20 '26 20:09

ONils


1 Answers

You could use reset_index(drop=True), since A's index is default?

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

Or,

In [128]: pd.Series(A.values + B.values, index=A.index)
Out[128]:
0    2
1    4
2    6
dtype: int64
like image 88
Zero Avatar answered Sep 22 '26 10:09

Zero