import pandas as pd
x = pd.DataFrame({'cat': ('A', 'A', 'B'), 'val': (10, 20, 30)})
labels, levels = pd.factorize(x['cat'])
x['cat'] = labels
x = x.set_index('cat')
print(x)
pandas equivalent of Stata's encode