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
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
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