Amelio Vazquez-Reina
Amelio Vazquez-Reina

Reputation: 96300

Conditional creation of a series from other series

Say I have two series s1 and s2 where s2 includes all indices of s1 and a few more.

I would like to obtain a third series s3 with the same indices as s2 where:

How can I do this in Pandas? I first thought I could do this with a join or concat operation, but I don't think that would work.

Upvotes: 1

Views: 41

Answers (1)

Andy Hayden
Andy Hayden

Reputation: 375575

You can use update to do this inplace:

In [11]: s1 = pd.Series([1, 2])

In [12]: s2 = pd.Series([3, 4, 5])

In [13]: s3 = s2.copy()  # update is inplace, so let's do this on a copy

In [14]: s3.update(s1)

In [15]: s3
Out[15]:
0    1
1    2
2    5
dtype: int64

Upvotes: 2

Related Questions