I'm trying to turn a column of strings into integer identifiers...and I cannot find an elegant way of doing this in pandas (or python). In the following example, I transform "A", which is a column/variable of strings into numbers through a mapping, but it looks like a dirty hack to me
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': ['homer_simpson', 'mean_street', 'homer_simpson', 'bla_bla'], 'B': 4})
unique = df['A'].unique()
mapping = dict(zip(unique, np.arange(len(unique))))
new_df = df.replace({'A': mapping})
Is there a better, more direct, way of achieving this?