0

So if I have a column of a pandas dataframe that looks like this :

x

FWD

MID

DEF

GK

MID

Is there anyway I can convert these strings to numbers with identical values taking the same number like this :

x

0

1

2

3

1

  • Does this answer your question? [Pandas: convert categories to numbers](https://stackoverflow.com/questions/38088652/pandas-convert-categories-to-numbers) – Maximilian Peters Nov 24 '20 at 20:36

1 Answers1

0

You can rank your column (assuming df is your input data frame)

df["x"] = df["x"].rank(method='dense').astype(int)

Ref. https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.rank.html

Grzegorz Skibinski
  • 12,624
  • 2
  • 11
  • 34