pandas 替换一列中的多个值 [英] pandas replace multiple values one column

查看:47
本文介绍了 pandas 替换一列中的多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 ?

推荐答案

您的替换格式已关闭

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]

这篇关于 pandas 替换一列中的多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆