('python-pandas-how-to-map-string-to-number').replace('-', ' ').capitalize()
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
Using map
df.term.map({'3 yrs': 3, '5 yrs': 5})
Using replace
df.term.replace({'3 yrs': 3, '5 yrs': 5})