Python 熊猫替换一列的多个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22100130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:13:14  来源:igfitidea点击:

pandas replace multiple values one column

pythonpandasreplace

提问by ArtDijk

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

在列 risklevels 中,我想用 1 代替 Small,用 5 代替 Medium,用 15 代替 High。我试过:

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

But only the medium were replaced. What is wrong ?

但只更换了介质。怎么了 ?

回答by EdChum

You could define a dict and call map

您可以定义一个字典并调用 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]

回答by Jeff

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]

回答by Surya

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

回答by Antonio

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

字符串将每个字符串(小、中、高)替换为新字符串 (1,5,15)\

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

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

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

回答by ChrisDanger

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

看起来 OP 可能一直在寻找一种单行程序来通过连续调用 '.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.

OP,你很接近但只需要用 .str.replace 替换你的逗号,并且不需要字典格式的列调用(“风险”)。只需将匹配模式和替换值作为要替换的参数传递即可。

回答by Mehdi Rostami

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

我必须打开“regex”标志才能使其工作:

df.replace({'a' : { 'Medium' : 2, 'Small' : 1, 'High' : 3 }}, regex=True)

df.replace({'a' : { 'Medium' : 2, 'Small' : 1, 'High' : 3 }}, regex=True)