0

I'm making a model that predicts whether an invidual will buy a product after watching an ad.

Here's the data, (sorry for the size I don't know how to make it smaller):

Data

I want to add a new column called AgeRange, where the value is 0 if age < 27, 1 if 27 <= age < 53, and 2 if age >= 53.

So far I've done this:

eng_data = clean_data.copy()
eng_data['AgeRange'] = [0 if i < 27 else 1 for i in eng_data['Age']]

but I'm not sure how to add the value 2 if age >= 53.

Thanks in advance.

R_C
  • 35
  • 5

2 Answers2

1

You can use apply and lambda function to achieve this in a simple means:

eng_data['AgeRange'] = eng_data['Age'].apply(lambda x: 0 if x < 27  else (1 if x < 53 else 2))
SM1312
  • 516
  • 4
  • 13
0

You can simply use nested if

eng_data['AgeRange'] = [0 if i < 27 else (1 if (i>=27 and i<53) for i in eng_data['Age']]
im_vutu
  • 412
  • 2
  • 9