Search code examples
pythonpandasdataframefeature-engineering

How do I feature engineer more than 2 new variables in a pandas dataframe?


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.


Solution

  • 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']]