('python-pandas-how-to-map-string-to-number').replace('-', ' ').capitalize()
'Python pandas how to map string to number'

Python pandas how to map string to number

import numpy as np
import pandas as pd
df = pd.DataFrame({'id': list('ABCDE'), 'term': np.random.choice(['3 yrs', '5 yrs'], 5)})
df
id term
0 A 5 yrs
1 B 5 yrs
2 C 5 yrs
3 D 3 yrs
4 E 5 yrs

Using map

df.term.map({'3 yrs': 3, '5 yrs': 5})
0    5
1    5
2    5
3    3
4    5
Name: term, dtype: int64

Using replace

df.term.replace({'3 yrs': 3, '5 yrs': 5})
0    5
1    5
2    5
3    3
4    5
Name: term, dtype: int64