python-small-examples

@author jackzhenguo
@desc category列转数值
@tag
@version 
@date 2020/03/18

第 180 个小例子:category列转数值

某列取值只可能为有限个枚举值,往往需要转为数值,使用get_dummies,或自己定义函数:

pd.get_dummies(df['a'])

自定义函数,结合 apply:

def c2n(x):
    if x=='A':
        return 95
    if x=='B':
        return 80

df['a'].apply(c2n)
[上一个例子](/python-small-examples/md/179.html) [下一个例子](/python-small-examples/md/181.html)