Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 76 ยท Exploratory Data Analysis

Looking at One Variable at a Time

Explore a single variable with histograms, density curves, boxplots and summary tables: choose bin counts, read skewness and spot outliers and odd spikes.

Univariate analysis means studying one variable by itself: what values does it take, how often, around what center and with what spread? It is the second pass of the EDA process, and it is the cheapest way to find trouble. A histogram can reveal a mix of two populations, a pile-up at zero, or a value that is physically impossible, all before you have modelled anything.

For each variable, ask the same few questions. Where is the center? How wide is the spread? Is the shape symmetric or skewed? Are there gaps, spikes or extreme values? The tools below answer them: summary numbers, histograms, density curves and boxplots.

flowchart TD
    V["One variable"] --> T{"Type?"}
    T -->|"numeric"| N["Histogram + boxplot<br/>mean, median, spread, skewness"]
    T -->|"categorical"| C["Counts and shares<br/>bar chart"]
    N --> Q["Questions: center, spread, shape, outliers, spikes"]
    C --> Q2["Questions: levels, rare values, spelling variants"]

The right tools depend on whether the variable is a number or a category.

Summary numbers

Start with describe(), then add what it leaves out. Useful measures:

  • Center: mean and median. When they differ a lot, the data is skewed or has outliers.
  • Spread: standard deviation, the interquartile range (IQR = 75th percentile minus 25th), minimum and maximum.
  • Shape: skewness measures asymmetry. Roughly 0 is symmetric, positive means a long right tail (a few very large values), negative means a long left tail. As a rough guide, absolute values above about 1 deserve attention.
  • Missing and distinct counts: how many values are missing, how many distinct values exist.

For details on these statistics see Descriptive Statistics in Practice.

Python A reusable univariate summary

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Look at income: the mean is clearly above the median and the skewness is positive and large, the usual signature of money. age has mean and median almost equal and skewness near zero. visits is a count: a small positive skew.

Histograms

A histogram cuts the range of a numeric variable into bins and counts how many values fall in each. The bar height is the count (or, in a density histogram, a share). It shows shape at a glance: one hump or two, symmetric or lopsided, gaps, spikes.

Choosing the number of bins

Bins are the one decision that changes what you see:

  • Too few bins smooth everything and hide structure.
  • Too many bins make the chart jagged, so random noise looks like features.

There is no single right answer. Defaults such as bins="auto" in NumPy and matplotlib use rules (Sturges, Freedman-Diaconis) that give a sensible start, which you adjust by looking. Always try at least two settings before you believe a shape. Keep bin widths "round" (for example, 5 years rather than 4.7) so people can read the axis.

Python The same data with three bin counts

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

With 4 bins the second group disappears into a single block. With 300 bins every bar holds a handful of values and the picture is noise. Around 30 bins, the two humps are clear: a clue that the variable mixes two kinds of users, which a single mean would hide.

Density curves

A kernel density estimate (KDE) draws a smooth curve through the data, a histogram without the bin edges. It makes the shape easy to compare across groups, since curves overlap better than bars. The smoothing has a bandwidth, which plays the same role as bin width: too wide hides humps, too narrow wiggles. SciPy offers scipy.stats.gaussian_kde; pandas offers Series.plot.kde(). Never read exact values off a KDE; it can also spill past real limits (such as negative ages).

Boxplots

A boxplot summarizes a distribution in five numbers: the box runs from the 25th to the 75th percentile with a line at the median; the whiskers reach the most extreme points within 1.5 IQR of the box; points beyond that are drawn individually and called outliers by this rule. Boxplots are compact, so they are ideal for comparing many variables or groups side by side, but they cannot show two humps. Pair them with a histogram.

Python Histogram plus boxplot for a skewed variable

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

On the raw scale the mean sits to the right of the median, the boxplot shows many dots beyond the upper whisker, and skewness is clearly positive. After taking logs the shape is almost symmetric and the dots nearly vanish. The "outliers" of the raw income were mostly the normal long tail of a skewed variable, not errors. Always ask why a point is extreme before deleting it (Outliers, Errors and Impossible Values).

Categorical variables

For categories, look at counts and shares with value_counts() (add normalize=True for shares) and a bar chart sorted by size. Check:

  • the number of distinct levels (a "category" with 900 levels is probably an id);
  • the share of the largest level (is the column nearly constant?);
  • rare levels that could be merged into "Other";
  • spelling variants such as "NY", "ny" and "New York" (Cleaning Text and Categories).

Do not use pie charts for more than a few categories; a sorted horizontal bar chart is easier to read.

Common things you will spot

What you see Possible meaning Typical action
A tall spike at 0 or at -1 Missing value coded as a number Recode as missing
A spike at exactly the median or mean Missing values already filled in Ask how it was imputed
Two humps Two populations mixed Split by a group variable
Heavy right tail Natural skew (money, durations) Log transform, use median
Values that cannot exist Entry error or unit mix-up Investigate, correct or remove
Round-number heaping (ages 20, 25, 30) People round when reporting Bin more coarsely

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A variable has mean 52,000 and median 38,000, with skewness +2.1. What does this suggest?

    A mean well above the median and a large positive skewness indicate a few very large values stretching the right tail.
  2. 2. What is the risk of using too many histogram bins?

    With many bins each bar holds few values, so chance variation creates a jagged picture. Too few bins have the opposite problem and hide structure.
  3. 3. In a standard boxplot, what do the dots beyond the whiskers represent?

    The 1.5 IQR rule only flags unusual points. They may be errors or perfectly valid values in a skewed distribution.
  4. 4. A histogram of session length shows two separate humps. What should you suspect?

    Two humps usually mean two groups, such as casual and heavy users. Look for a variable that separates them.
  5. 5. Why try more than one bin setting?

    Bin choices change what you see. Conclusions should hold up across reasonable settings.

Summary

  • Univariate analysis checks center, spread, shape and oddities of each variable separately.
  • Compare mean and median and read skewness; a large gap signals a skewed tail.
  • Histograms need a thoughtful number of bins: try several. KDE curves smooth the same idea.
  • Boxplots are compact and good for comparison but cannot show multiple humps; pair them with histograms.
  • Count levels, shares and rare values for categories; look for spikes, heaping and impossible values.

Next: Relationships Between Variables, where two or more columns are examined together.