How to convert certain categorical values from a DataFrame to numerical(int) in python? -
i have dataframe multiple columns , categorical data in want assign numerical (int) value in order proceed data clean-up need do.
e.g. want cells in column oldvalue & newvalue containing "1st call" have value of 2, "2nd call" have value of 3, , on...
i post screenshot of dataframe understand mean.
i new programming languages hence if please put practical example answer of huge help.
you may use replace , passing dictinary maps each category on numerical value , add new column dataframe:
df['oldvalueint'] = df['oldvalue'].replace( {'1st call attempted': 2, '2nd call attempted': 3})
example:
df = pd.dataframe([['a','x'],['b','x'],['a','y']], columns=['ab','xy']) df['abint'] = df['ab'].replace('a': 1, 'b': 2) print df
which yields
ab xy abint 0 x 1 1 b x 2 2 y 1
or if want replace multiple columns:
df[['ab','xy']] = df.replace( {'ab': {'a': 1, 'b': 2}, 'xy': {'x': 2, 'y': 3}} )
Comments
Post a Comment