pandas.factorize on an entire data frame

后端 未结 3 1336
青春惊慌失措
青春惊慌失措 2020-12-07 15:13

pandas.factorize encodes input values as an enumerated type or categorical variable.

But how can I easily and efficiently convert many columns of a data frame? What

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-07 15:35

    I also found this answer quite helpful: https://stackoverflow.com/a/20051631/4643212

    I was trying to take values from an existing column in a Pandas DataFrame (a list of IP addresses named 'SrcIP') and map them to numerical values in a new column (named 'ID' in this example).

    Solution:

    df['ID'] = pd.factorize(df.SrcIP)[0]
    

    Result:

            SrcIP | ID    
    192.168.1.112 |  0  
    192.168.1.112 |  0  
    192.168.4.118 |  1 
    192.168.1.112 |  0
    192.168.4.118 |  1
    192.168.5.122 |  2
    192.168.5.122 |  2
    ...
    

提交回复
热议问题