54

In a column risklevels I want to replace Small with 1, Medium with 5 and High with 15. I tried:

dfm.replace({'risk':{'Small': '1'}},
            {'risk':{'Medium': '5'}},
            {'risk':{'High': '15'}})

But only the medium were replaced. What is wrong ?

buhtz
  • 10,774
  • 18
  • 76
  • 149
ArtDijk
  • 1,957
  • 6
  • 23
  • 31

7 Answers7

90

Your replace format is off

In [21]: df = pd.DataFrame({'a':['Small', 'Medium', 'High']})

In [22]: df
Out[22]: 
        a
0   Small
1  Medium
2    High

[3 rows x 1 columns]

In [23]: df.replace({'a' : { 'Medium' : 2, 'Small' : 1, 'High' : 3 }})
Out[23]: 
   a
0  1
1  2
2  3

[3 rows x 1 columns]
Jeff
  • 125,376
  • 21
  • 220
  • 187
  • 3
    I wasn't sure what was wrong with the `replace` format line so I suggested using `map` instead. +1 for spotting OP error – EdChum Feb 28 '14 at 16:34
  • This solution only works in the current example when the cell in the dataframe contains one word. If there is more than one word ```re.sub()``` worked for me. Used single commands / code lines. I am sure there is a fancier solution (maybe with [translate](https://note.nkmk.me/en/python-str-replace-translate-re-sub/#replace-multiple-different-substrings) ?) But it's a time management question... – Simone Jul 05 '23 at 12:36
28
In [123]: import pandas as pd                                                                                                                                

In [124]: state_df = pd.DataFrame({'state':['Small', 'Medium', 'High', 'Small', 'High']})                                                                    

In [125]: state_df
Out[125]: 
    state
0   Small
1  Medium
2    High
3   Small
4    High

In [126]: replace_values = {'Small' : 1, 'Medium' : 2, 'High' : 3 }                                                                                          

In [127]: state_df = state_df.replace({"state": replace_values})                                                                                             

In [128]: state_df
Out[128]: 
   state
0      1
1      2
2      3
3      1
4      3
Surya
  • 11,002
  • 4
  • 57
  • 39
12

You could define a dict and call map

In [256]:

df = pd.DataFrame({'a':['Small', 'Medium', 'High']})
df
Out[256]:
        a
0   Small
1  Medium
2    High

[3 rows x 1 columns]
In [258]:

vals_to_replace = {'Small':'1', 'Medium':'5', 'High':'15'}
df['a'] = df['a'].map(vals_to_replace)
df
Out[258]:
    a
0   1
1   5
2  15

[3 rows x 1 columns]


In [279]:

val1 = [1,5,15]
df['risk'].update(pd.Series(val1))
df
Out[279]:
  risk
0    1
1    5
2   15

[3 rows x 1 columns]
EdChum
  • 376,765
  • 198
  • 813
  • 562
8

Looks like OP may have been looking for a one-liner to solve this through consecutive calls to .str.replace:

dfm.column = dfm.column.str.replace('Small', '1') \
    .str.replace('Medium', '5') \
        .str.replace('High', '15')

OP, you were close but just needed to replace your commas with .str.replace and the column call ('risk') in a dictionary format isn't necessary. Just pass the pattern-to-match and replacement-value as arguments to replace.

buhtz
  • 10,774
  • 18
  • 76
  • 149
ChrisDanger
  • 1,071
  • 11
  • 10
  • 7
    Welcome to Stack Overflow. Please could you add some explanation to your answer? (What changes did you make, and why? Why did the OPs original code not work?) Without the explanation, the answer isn't so useful to future visitors. – Mark Dickinson Aug 31 '18 at 17:47
  • 3
    What does this answer add that the other answers lack? – NickD Aug 31 '18 at 18:31
  • 1
    This answer is useful if you want to replace a piece of the string and not the entire string. I found this answer when trying to understand if you could put together multiple .str.replace in a statement. That said, if the match is desired to the total string (OPs question), and not a piece of the string, the preferred answer is best. – Jeff D. White May 27 '20 at 22:38
6

I had to turn on the "regex" flag to make it work:

 df.replace({'a' : {'Medium':2, 'Small':1, 'High':3 }}, regex=True)
Amir Pourmand
  • 519
  • 6
  • 17
Mehdi Rostami
  • 139
  • 2
  • 5
2

String replace each string (Small, Medium, High) for the new string (1,5,15)\

If dfm is the dataframe name, column is the column name.

dfm.column = dfm.column.str.replace('Small', '1')
dfm.column = dfm.column.str.replace('Medium', '5')
dfm.column = dfm.column.str.replace('High', '15')
Antonio
  • 689
  • 6
  • 4
1

Use series.replace with lists of before and after values for greater ease:

df.risklevels = df.risklevels.replace( ['Small','Medium','High'], [1,2,3] )

See here.

markling
  • 1,232
  • 1
  • 15
  • 28