Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas replace multiple values one column

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 ?

like image 753
ArtDijk Avatar asked Feb 28 '14 16:02

ArtDijk


People also ask

How do I change multiple values in a column in pandas?

Pandas replace multiple values in column replace. By using DataFrame. replace() method we will replace multiple values with multiple new strings or text for an individual DataFrame column. This method searches the entire Pandas DataFrame and replaces every specified value.

How do you replace multiple values in a list Python?

Replace Multiple Values in a Python List. There may be many times when you want to replace not just a single item, but multiple items. This can be done quite simply using the for loop method shown earlier.

How do you replace all values in a data frame?

Pandas DataFrame replace() Method The replace() method replaces the specified value with another specified value. The replace() method searches the entire DataFrame and replaces every case of the specified value.


3 Answers

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]
like image 168
Jeff Avatar answered Oct 17 '22 14:10

Jeff


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
like image 21
Surya Avatar answered Oct 17 '22 12:10

Surya


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]
like image 10
EdChum Avatar answered Oct 17 '22 13:10

EdChum