Rename columns with special signs and numbers

Question:

I have this

df = pd.DataFrame( {'st': {0: '0', 1: '>0-<10', 2: '>=10-<20', 3: '>=20'}} )

which yields

       st
    0
    >0-<10
    >=10-<20
    >=20

Each entry of [st] column represents a category.

I would like to replace categories as follows:

   st
    1
    2
    3
    4

to achieve this, I tried:

dic = {
'0':'1',
'>0-<10':'2',        
'>=10-<20':'3', 
'>=20':'4'}

df['st'] = df['st'].replace(dic, regex=True)

I also tried

conditions =["0",
    ">0-<10",
    ">=10-<20",
    ">=20"]

choices =["1",
    "2",
    "3",
    "4"]

df["st2"]=np.select(conditions,choices)

Unfortunately, neither of these code solutions yield to desired output.

Asked By: josepmaria

||

Answers:

Convert the column to category dtype and get codes with cat.codes. This will number the categories starting from 0, so you may also add 1:

df['st'] = df['st'].astype('category').cat.codes + 1
Answered By: Nuri Taş
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.