Python Central HubSign in

Data Analytics with Python / Phase 4: Data Preprocessing and Cleaning

Feature Engineering Basics

Create better signals from raw columns - extracting datetime parts, ratios, flags, and aggregated features.

What is feature engineering?

Feature engineering is the process of converting raw data into useful inputs (features).

Examples:

  • Date → day of week
  • Amount → log(amount)
  • Text → length, presence of keywords
  • Customer transactions → total spend, avg order value

Example: derive features from datetime

Python
import pandas as pd
 
df = pd.DataFrame({
    "order_time": pd.to_datetime([
        "2025-01-01 10:15:00",
        "2025-01-02 18:30:00",
        "2025-01-03 09:10:00",
    ]),
    "amount": [250, 180, 90],
})
 
df["hour"] = df["order_time"].dt.hour
df["weekday"] = df["order_time"].dt.day_name()
df["is_weekend"] = df["order_time"].dt.weekday >= 5
 
print(df)

Example: ratios and flags

Python
import pandas as pd
 
df = pd.DataFrame({"revenue": [1000, 500], "users": [100, 20]})
 
df["revenue_per_user"] = df["revenue"] / df["users"]
df["is_high_value"] = df["revenue_per_user"] > 20
 
print(df)

Example: aggregated features (groupby)

Python
import pandas as pd
 
orders = pd.DataFrame({
    "customer_id": [1, 1, 2, 2, 2],
    "amount": [100, 250, 90, 180, 300],
})
 
cust = orders.groupby("customer_id").agg(
    total_spend=("amount", "sum"),
    avg_order=("amount", "mean"),
    orders=("amount", "count"),
).reset_index()
 
print(cust)

Guiding principles

  • Don’t leak target/future information.
  • Prefer simple features first.
  • Validate with charts and summary stats.
  • Document each feature and its meaning.

Visualize it

diagram Feature engineering flow mermaid
Raw columns are transformed and combined into new, more useful features for modeling.

🧪 Try It Yourself

Exercise 1 – Derive a feature with map

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Extract datetime parts

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Aggregate a feature per group

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Check your understanding

quizCheck yourself
  1. Which example turns a datetime into a useful feature?

    Show answer

    A — df["order_time"].dt.hour — Extracting hour, weekday or is_weekend from a datetime gives models something to learn from.

  2. With revenue 1000 and 500 and users 100 and 20, which rows have revenue_per_user greater than 20?

    Show answer

    A — Only the second row, at 25 — The ratios are 1000/100 = 10 and 500/20 = 25, so only the second row is flagged as high value.

  3. For customer 2 with orders 90, 180 and 300, what do total_spend, avg_order and orders come out as?

    Show answer

    A — 570, 190.0 and 3 — 90 + 180 + 300 = 570, the mean is 190 and there are 3 orders.

  4. Which guiding principle does the lesson give about the target?

    Show answer

    A — Do not leak target or future information into features — Leaking the target or the future makes a model look great in testing but fail in real use.

Next

You've now covered the whole phase — wrap it up by making sure raw columns have the right Data Type Conversion and Validation before any of these features are built.

Was this page helpful?

Let us know how we did