Home > Mobile >  How can I set the value of a Series at a specific in a chainable style?
How can I set the value of a Series at a specific in a chainable style?

Time:12-09

I can't figure how to set the value of a Series at a specific index in a chainable style.

For example, say I have the following dataframe:

>>> df = pd.DataFrame({'a': [1,2,3], 'b': [0,0,0]})
>>> df
   a  b
0  1  0
1  2  0
2  3  0

If I want to change all the values of a column in a pipeline, I can use pandas.DataFrame.assign():

>>> df.assign(b=[4,5,6])
   a  b
0  1  4
1  2  5
2  3  6

...and then I can do other stuff with the dataframe on the same line, for example:

>>> df.assign(b=[4,5,6]).mul(100)
     a    b
0  100  400
1  200  500
2  300  600

But I can't do this for an individual value at a specific in a Series.

>>> s = df['a']
>>> s
0    1
1    2
2    3
Name: a, dtype: int64

I can, of course, just use a normal Python assignment operation using =:

>>> s[1] = 9
>>> s
0    1
1    9
2    3
Name: a, dtype: int64

But the problems with that are:

  • It's in-place, so it modifies my existing dataframe
  • Assignment statements using = are not allowed in Python lambda functions

For example, what if I wanted to do this:

>>> df.apply(lambda x: x['b', 0] = 13, axis=1)
  File "<stdin>", line 1
    df.apply(lambda x: x['b', 0] = 13, axis=1)
             ^
SyntaxError: expression cannot contain assignment, perhaps you meant "=="?

(I understand that there are better ways to that particular case, but this is just a made-up example.)

How can I set the value at the specified index of a Series? I would like to be able to just to something like s.set_value(idx, 'my_val') and have it return the modified (copied) Series.

CodePudding user response:

Perhaps you are looking for:

df.eval('b = 13')

And to change values in a Series use:

my_series.replace(to_replace=0, value=13)

It's a powerful function with some additional parameters you might want to use for more flexibility: https://pandas.pydata.org/docs/reference/api/pandas.Series.replace.html

Replace uses values rather than indices so consider where too: https://pandas.pydata.org/docs/reference/api/pandas.Series.where.html#pandas.Series.where

>>> s = pd.Series({2: 23, 5: 25, 235:62})
>>> s.where(s.index != 5, 66)
2      23
5      66
235    62
dtype: int64
  • Related