Pandas - make a column dtype object or Factor

前端 未结 3 1500
北荒
北荒 2020-12-24 05:09

In pandas, how can I convert a column of a DataFrame into dtype object? Or better yet, into a factor? (For those who speak R, in Python, how do I as.factor()?)<

3条回答
  •  无人及你
    2020-12-24 05:25

    There's also pd.factorize function to use:

    # use the df data from @herrfz
    
    In [150]: pd.factorize(df.b)
    Out[150]: (array([0, 1, 0, 1, 2]), array(['yes', 'no', 'absent'], dtype=object))
    In [152]: df['c'] = pd.factorize(df.b)[0]
    
    In [153]: df
    Out[153]: 
       a       b  c
    0  1     yes  0
    1  2      no  1
    2  3     yes  0
    3  4      no  1
    4  5  absent  2
    

提交回复
热议问题