Convert array of string (category) to array of int from a pandas dataframe

前端 未结 4 1608
孤城傲影
孤城傲影 2020-12-14 11:25

I am trying to do something very similar to that previous question but I get an error. I have a pandas dataframe containing features,label I need to do some convertion to se

4条回答
  •  没有蜡笔的小新
    2020-12-14 12:16

    I am answering the question for Pandas 0.10.1. Factor.from_array seems to do the trick.

    >>> s = pandas.Series(['a', 'b', 'a', 'c', 'a', 'b', 'a'])
    >>> s
    0    a
    1    b
    2    a
    3    c
    4    a
    5    b
    6    a
    >>> f = pandas.Factor.from_array(s)
    >>> f
    Categorical: 
    array([a, b, a, c, a, b, a], dtype=object)
    Levels (3): Index([a, b, c], dtype=object)
    >>> f.labels
    array([0, 1, 0, 2, 0, 1, 0])
    >>> f.levels
    Index([a, b, c], dtype=object)
    

提交回复
热议问题