I have a pandas dataframe and and having a column age
. I want to encode it into categorical values separated by specific range, for example, ages below 15 should be 0, between 15 and 30 should be changed to 1 and so on.
I found this way to do this(after going through a huge confusion about the use of &
and and
)
age = X.loc[:, 'Age']
age[ age<15 ] = 0
age[ (15<age) & (age<=30) ] = 1
age[ (30<age) & (age<=50) ] = 2
age[ (50<age) & (age<=80) ] = 3
Is this the best way to so this? Can I do this, for example with LabelEncoder?