0

I have a dataset consisting of 382 rows and 4 columns. I need to convert all the names in to numbers. The names do repeat here and there, so I can't just randomly give numbers. So, I made a dictionary of the names and the corresponding values. But now, I am not able to change the values in the column. This is how I tried to add the values to the column:

test_df.replace(to_replace = d_loc,value = None, regex = True, inplace = True)
print(test_df)

but test_df just gives me the same dataframe, without any modifications. What should I use? I have over 100 unique names, so I cannot mannually rename them.

skuzzy
  • 541
  • 1
  • 3
  • 14
  • 1
    This seems like it's just [`pandas.factorize`](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.factorize.html)? – roganjosh Dec 19 '20 at 10:15

1 Answers1

0

df.applymap() works on each item in a dataframe:

test_df.applymap(lambda x: dict_to_replace[x])