TutorialsPythonFeature Engineering for Analysts

Feature Engineering for Analysts

Create new meaningful columns from existing data to improve analysis and modelling

Feature engineering is the process of creating new columns (features) from existing data to make analysis more meaningful. For data analysts, this means: extracting date parts, computing ratios, creating category bins, and deriving business metrics that are not in the raw data. Good features often reveal insights that raw columns hide — for example, "day of week" from a timestamp, "days since last purchase" from a date, or "revenue per unit" from two separate columns.

Example

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

df = pd.read_csv("orders.csv", parse_dates=["OrderDate"])

# DATE FEATURES
df["Year"]       = df["OrderDate"].dt.year
df["Month"]      = df["OrderDate"].dt.month
df["Quarter"]    = df["OrderDate"].dt.quarter
df["DayOfWeek"]  = df["OrderDate"].dt.dayofweek  # 0=Mon, 6=Sun
df["IsWeekend"]  = (df["DayOfWeek"] >= 5).astype(int)
df["DaySinceStart"] = (df["OrderDate"] - df["OrderDate"].min()).dt.days

# RATIO FEATURES
df["Margin_Pct"] = (df["Profit"] / df["Revenue"] * 100).round(2)
df["Rev_Per_Unit"] = (df["Revenue"] / df["Units"]).round(2)
df["Return_Rate"]  = df["Returns"] / df["Orders"]

# BINNING — convert continuous to category
df["Sales_Band"] = pd.cut(
    df["Revenue"],
    bins=[0, 100000, 500000, 1000000, float("inf")],
    labels=["Small", "Medium", "Large", "Enterprise"],
)

# INTERACTION FEATURES
df["Sales_Per_Ad_Spend"] = df["Revenue"] / df["Ad_Spend"].replace(0, np.nan)

# LAG FEATURES (time series)
df = df.sort_values("OrderDate")
df["Prev_Month_Sales"] = df["Revenue"].shift(1)   # previous row value
df["MoM_Change"] = df["Revenue"] - df["Prev_Month_Sales"]
💡 Feature engineering for analysis differs from ML feature engineering — focus on business-meaningful metrics, not statistical transformations.

Key Points

  • Extract date parts (year, month, quarter, day of week) from timestamp columns
  • Compute ratio features: margin %, revenue per unit, return rate
  • pd.cut() creates categorical bins from continuous numeric data
  • shift(1) creates a lagged column — the previous row's value
  • Replace 0 with NaN before computing ratios to avoid division-by-zero errors

Practice Question

You want to flag whether an order was placed on a weekend. The DayOfWeek column has 0=Monday to 6=Sunday. Which code creates an IsWeekend binary flag?

Related Topics

Pandas DataFramesThe core Pandas data structure — a 2D table with rows and columnsString and Date Operations in PandasClean text columns and extract date parts with Pandas str and dt accessorsCapstone Project — Sales AnalysisEnd-to-end data analysis project: load, clean, analyse and visualise sales data