df.target_column_name = pd.Categorical(df.target_column_name) df[target_column_name] = df.target_column_name.cat.codes