I am converting strings to categorical values in my dataset using the following piece of code.
data[\'weekday\'] = pd.Categorical.from_array(data.weekday).labels
A simple & elegant way to do the same.
cat_list = ['Sun', 'Sun', 'Wed', 'Mon', 'Mon'] encoded_data, mapping_index = pd.Series(cat_list).factorize()
and you are done, check below
print(encoded_data) print(mapping_index) print(mapping_index.get_loc("Mon"))