Replace values in pandas Series with dictionary

后端 未结 1 1751
后悔当初
后悔当初 2021-01-18 15:02

I want to replace values in a pandas Series using a dictionary. I\'m following @DSM\'s accepted answer like so:

s = Series([\'abc\', \'abe\', \'         


        
1条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-18 15:55

    You can do it using regex=True parameter:

    In [37]: s.replace(d, regex=True)
    Out[37]:
    0    aBc
    1    aBe
    2    aBg
    dtype: object
    

    As you have already found out yourself - it's a RegEx replacement and it won't work as you expected:

    In [36]: s.replace(d)
    Out[36]:
    0    abc
    1    abe
    2    abg
    dtype: object
    

    this is working as expected:

    In [38]: s.replace({'abc':'ABC'})
    Out[38]:
    0    ABC
    1    abe
    2    abg
    dtype: object
    

    0 讨论(0)
提交回复
热议问题