I have a pandas dataframe like so:
x = pd.DataFrame({'col1':['one','two','three','four'],'col2':[5,6,7,8],'col3':[9,10,11,12]})
For my purposes (training a ml model, I need to replace the text with numbers, so I use pd.replace() with a dictionary to change that
mydict = {'one': 1, 'two': 2, 'three': 3, 'four': 4}
x.replace({'col1':mydict}, inplace= True)
After that, I train the model and have it return a proposed candidate, but the model, having seen only the numbers, returns the candidate as numbers in that first column, something like this
col1 | col2 | col3 |
---|---|---|
1 | 5 | 9 |
Where I'd like to get something like this
col1 | col2 | col3 |
---|---|---|
one | 5 | 9 |
I've seen this question where they create an inverted dictionary to solve the problem, and this one about getting the values of a python dictionary. But I'd like to avoid having to create another dictionary, seeing as the values of the dictionary are as unique as the keys.
I get the feeling there should be some easy way of looking up the values as if they were the keys and doing the replacement like that, but I'm not sure.