5/14
Feature Engineering with Apply · Page 1 of 1

The .apply() Method

Feature Engineering with Apply

Beyond Simple Math

While you can do df['col'] * 2, sometimes you need complex logic (if/else, string manipulation, external function calls) to create new features.

How apply() works

It passes every row (or column) to a function you define.

def categorize_gpa(gpa):
    if gpa >= 3.7: return "Honors"
    elif gpa >= 3.0: return "Good"
    else: return "Needs Improvement"

df['status'] = df['gpa'].apply(categorize_gpa)

Row-wise Apply

Pass axis=1 to access multiple columns at once:

def calculate_total_score(row):
    return (row['exam'] * 0.7) + (row['quiz'] * 0.3)

df['final_score'] = df.apply(calculate_total_score, axis=1)

⚠️ apply() is slower than vectorized operations. Only use it when vectorized math (df['a'] + df['b']) is impossible.

main.py
Loading...
OUTPUT
Click "Run Code" to execute…