Skip to content

Feature Engineering Basics

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
Datetime features
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)
Ratios
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)
Aggregations
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)
  • Don’t leak target/future information.
  • Prefer simple features first.
  • Validate with charts and summary stats.
  • Document each feature and its meaning.
diagram Feature engineering flow mermaid
Raw columns are transformed and combined into new, more useful features for modeling.

Exercise 3 – Aggregate a feature per group

Section titled “Exercise 3 – Aggregate a feature per group”

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.

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading