Say I have a column in a dataframe which is 'user_age', and I have created 'user_age_bin' by something like:
df['user_age_bin']= pd.cut(df['user_age'], bins=[10, 15, 20, 25,30])
Then I build a machine learning model by using the 'user_age_bin' feature.
Next, I got one record which I need to throw into my model and make prediction. I don't want to use the user_age
as it is because the model uses user_age_bin
. So, how can I convert a user_age
value (say 28) into user_age_bin
? I know I can create a function like this:
def assign_bin(age):
if age < 10:
return '<10'
elif age< 15:
return '10-15'
... etc. etc.
and then do:
user_age_bin = assign_bin(28)
But this solution is not elegant at all. I guess there must be a better way, right?
Edit: I changed the code and added explicit bin range. Edit2: Edited wording and hopefully the question is clearer now.