I have a dataframe with this type of data (too many columns):
col1 int64 col2 int64 col3 category col4 category col5 categ
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