Convert categorical data in pandas dataframe

后端 未结 10 1924
予麋鹿
予麋鹿 2020-11-27 10:01

I have a dataframe with this type of data (too many columns):

col1        int64
col2        int64
col3        category
col4        category
col5        categ         


        
10条回答
  •  感动是毒
    2020-11-27 10:29

    You can do it less code like below :

    f = pd.DataFrame({'col1':[1,2,3,4,5], 'col2':list('abcab'),'col3':list('ababb')})
    
    f['col1'] =f['col1'].astype('category').cat.codes
    f['col2'] =f['col2'].astype('category').cat.codes
    f['col3'] =f['col3'].astype('category').cat.codes
    
    f
    

提交回复
热议问题