pandas将一列替换为多个值

时间:2021-06-02 07:53:33

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

在列冒险级别中,我想将Small替换为1,将Medium替换为5,将High替换为15.我尝试:

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

But only the medium were replaced. What is wrong ?

但只有媒体被替换了。哪里不对 ?

4 个解决方案

#1


22  

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]

#2


6  

You could define a dict and call map

你可以定义一个dict和调用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]

#3


2  

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

#4


0  

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

String替换新字符串(1,5,15)的每个字符串(小,中,高)

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

如果dfm是数据框名称,则列是列名称。

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

#1


22  

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]

#2


6  

You could define a dict and call map

你可以定义一个dict和调用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]

#3


2  

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

#4


0  

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

String替换新字符串(1,5,15)的每个字符串(小,中,高)

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

如果dfm是数据框名称,则列是列名称。

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