[英]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:在列 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?怎么了?
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]
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
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]
Looks like OP may have been looking for a one-liner to solve this through consecutive calls to .str.replace
:看起来 OP 可能一直在寻找一种.str.replace
来通过连续调用.str.replace
来解决这个.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. OP,你很接近但只需要用.str.replace
替换你的逗号,并且不需要字典格式的列调用(“风险”)。 Just pass the pattern-to-match and replacement-value as arguments to replace.只需将匹配模式和替换值作为要替换的参数传递即可。
我必须打开“regex”标志才能使其工作:
df.replace({'a' : {'Medium':2, 'Small':1, 'High':3 }}, regex=True)
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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.