liveBook · Manning

Indicator variable

As explained in The Well-Grounded Data Analyst

An indicator variable is a variable that encodes whether a condition or category applies, typically with binary values such as zero and one.

An indicator variable is a derived column that marks whether a condition applies. It commonly contains 1 when a row matches and 0 otherwise, but it can also retain a relevant value—such as purchase price—and use 0 as the fallback. Summing the result isolates the count or value for the selected condition. section 4.4.2

Use indicators when raw categories cannot be treated as meaningful numeric scales. One-hot encoding creates one binary column per category, so each row records which category applies. For mutually exclusive categories, only one of those columns can be 1; this representation can serve as regression-model input. section 6.1.1

Monthly product metrics

For purchase events, create November and December count, revenue, and user columns. A November revenue indicator holds price for November purchases and 0 for all other rows; grouping by product and summing produces November revenue. Comparing the grouped monthly columns identifies products whose sales, revenue, or customer counts changed month to month. section 4.4.2

purchases
    .assign(
        november_count=np.where(purchases["event_time"].dt.month==11,
 1, 0),
        november_revenue=np.where(purchases["event_time"].dt.month==11, 
 purchases["price"], 0),
        november_user_id=np.where(purchases["event_time"].dt.month==11, 
 purchases["user_id"], np.nan),
        december_count=np.where(purchases["event_time"].dt.month==12,
 1, 0),
        december_revenue=np.where(purchases["event_time"].dt.month==12, 
 purchases["price"], 0),
        december_user_id=np.where(purchases["event_time"].dt.month==12, 
 purchases["user_id"], np.nan))

This pandas transformation builds month-specific count, revenue, and user indicators before they are aggregated by product. section 4.4.2

Multiple-choice survey responses

A semicolon-delimited survey answer cannot be analyzed directly as one category when respondents can select several options. Split it into one indicator column per option, then take each column’s mean to estimate the percentage of respondents who selected that option. Combine the indicators with survey attributes to compare use cases by sentiment, trust, or job role. section 6.4.2 section 7.2.3

How The Well-Grounded Data Analyst covers it

How deep it goes
Defines conditional and binary forms, then works through creation, aggregation, and crosstab analysis without detailing regression fitting.
Examples
Uses monthly purchase revenue and counts, categorical heart-disease slope values, and multi-select AI-tool survey use cases.
What you can do after
Apply it via pandas to create condition-specific metrics and split multi-select survey answers into analyzable binary columns.
How it connects
Connects one-hot encoding with categorical data, dummy variables, regression, grouping and aggregation, pivot tables, crosstabs, and heatmaps.

Also explained in

Compare how 4 books explain indicator variable →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Data Analyst. The section links point to the parts of the book it draws on.