0

I have a series, (call it x) which is generated according to the following logic:

  1. Extract x from y (x is a subset of y, both are Series)
  2. perform a number of manipulations on the elements of x

Now what I wnat to do is to put the elements of x (different values now) back to where they came from in y

To be concrete, here is (a subset of) what x and y look like:

>>> x         
2     0.026610
5     0.128612
8    -0.208833
11   -0.148410
14   -0.110574
17    0.187002


>>> y
0     0.259825
1     0.171325
2     0.195750
3     0.290775
4     0.319075
5     0.278700
6    -0.036125
7     0.102525
8    -0.044000
9    -0.539275
10   -0.573875
11   -0.008500
12    0.141325
13    0.186975
14    0.074875
15    0.079925
16    0.224075
17    0.358475
18   -0.107125

What I'm trying to do is to re-insert the elemnents of x back into y, based on their index. SO that, for example when I'm done, the y value associated with index=2 (currently 0.195750) would be replaced with the x value associated with index = 2 (0.026610).

Certainly I could brute force this, but looking for a better way.

A conditional list comprehension does at least put things in the right place, like so:

z = [y[i] if y.index[i] not in x.index else x[i] for i in range(len(y))]

But if I do the above, now I have a list and have the convert back to a Series, applying the index from y.

zS = pd.Series(z, index = y.index)

Could do that. I'm just wondering if there is some built in Pandas-magic that would get me there perhaps more cleanly and/or simply - going stright to a Series. Seems like there must be such a thing.

Thanks in advance.

1 Answer 1

1

How about update. Note that unlike many other pandas methods, it operates in place.

y.update(x)
Sign up to request clarification or add additional context in comments.

1 Comment

Doesn't get any slicker than that. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.