TutorialsPythonFeature Engineering in Python

Feature Engineering in Python

Create new meaningful columns from existing data — skills that increase your analytical value

Feature engineering is creating new columns (features) from existing data that are more useful for analysis or modelling. Examples: calculating profit margin from revenue and cost, extracting month from a date, creating age buckets from age numbers, flagging high-value customers. This skill is what differentiates a data analyst who just runs aggregations from one who adds analytical insight. Every meaningful KPI you add to a dashboard is a form of feature engineering.

Example

Common feature engineering transformations
import pandas as pd
import numpy as np

df = pd.read_csv("sales.csv")

# 1. DERIVED RATIOS
df["Profit_Margin"] = (df["Profit"] / df["Revenue"] * 100).round(2)
df["Discount_Rate"] = df["Discount"] / df["List_Price"]

# 2. BINNING (convert continuous to categories)
# pd.cut — equal width bins
df["Salary_Band"] = pd.cut(
    df["Salary"],
    bins=[0, 35000, 60000, 100000, float("inf")],
    labels=["Entry", "Junior", "Mid", "Senior"]
)
# pd.qcut — equal frequency bins (quartiles)
df["Salary_Quartile"] = pd.qcut(df["Salary"], q=4, labels=["Q1","Q2","Q3","Q4"])

# 3. DATE FEATURES
df["OrderDate"] = pd.to_datetime(df["OrderDate"])
df["Year"] = df["OrderDate"].dt.year
df["Month"] = df["OrderDate"].dt.month
df["DayOfWeek"] = df["OrderDate"].dt.dayofweek  # 0=Mon, 6=Sun
df["IsWeekend"] = df["DayOfWeek"].isin([5, 6])
df["Quarter"] = df["OrderDate"].dt.quarter

# 4. AGGREGATED FEATURES (customer-level metrics)
customer_stats = df.groupby("CustomerID").agg(
    total_spent=("Amount", "sum"),
    order_count=("OrderID", "count"),
    avg_order=("Amount", "mean")
).reset_index()
df = df.merge(customer_stats, on="CustomerID", how="left")

Key Points

  • pd.cut() creates equal-width bins; pd.qcut() creates equal-frequency bins (quartiles)
  • Date features (month, day of week, quarter) are some of the most valuable engineered features
  • Ratio features (margin %, discount rate) often reveal more than raw numbers
  • Group-level aggregates merged back to the row level are powerful features
  • Feature engineering is iterative — domain knowledge guides which new features are meaningful

Practice Question

You want to split a continuous Age column into 4 equal-sized groups (same number of rows in each group). Which pandas function should you use?