Skip to content
Hands-on Mentor
ENTR
Back to blog

Types of variables in statistics: categorical vs numerical data

Each column of a table is a variable with a type. Learn nominal, ordinal, discrete, and continuous variables, and why the type decides which math makes sense.

5 min read

The main types of variables in statistics are categorical (labels such as city or category) and numerical (numbers you can do math with, such as price or quantity). Data is a recorded set of observations, usually stored in a table: each row is one observation, and each column is one variable. The type of a variable decides which calculations make sense. You can average prices, but you cannot average cities.

What is a variable in data?

A variable is one property that you record for every observation. Here are five orders from Bazaar, a fictional online store:

order_idcitycategoryquantityamountsatisfaction
S00004Port AlderBooks1$15.954
S00010KingsbridgeBooks2$12.903
S00011NorthvaleClothing1$14.953
S00014Saint ClairBeauty1$21.455
S00021NorthvaleElectronics1$199.953
  • Row (observation): each order.
  • Column (variable): order ID, city, category, quantity, amount, and satisfaction.

What are categorical and numerical data?

Categorical data puts each observation into a group. Numerical data measures a quantity. Each has two sub-types:

TypeSub-typeExampleWhat you can do
NumericalContinuousamount ($15.95 is possible)Mean, median, standard deviation
NumericalDiscretequantity (2.5 items is not possible)Count, sum, mean
CategoricalNominal (no order)city, categoryCount, most common value (mode)
CategoricalOrdinal (ordered)satisfaction (1 to 5)Count, mode, median

A continuous variable can take any value in a range. A discrete variable takes separate values you can count.

What are nominal, ordinal, interval, and ratio variables?

These four names are a finer way to sort the same types, often called levels of measurement.

  • Nominal: names only, with no order. City and product category are nominal.
  • Ordinal: the categories have an order, but the gaps between them are not equal. A satisfaction score of 5 is better than 4, but you cannot say the step from 3 to 4 is the same size as the step from 4 to 5.
  • Interval: numbers with equal gaps but no true zero. Temperature in Celsius is the standard example: 20 degrees is not "twice as warm" as 10 degrees, because 0 is not "no heat".
  • Ratio: numbers with equal gaps and a true zero. Amount and quantity are ratio variables: $0 means no money, and $40 is twice $20.

Which columns look like numbers but are not?

The order_id column looks like a number, but it is a label. The average of order numbers means nothing. The same is true for postal codes, phone numbers, and student IDs. They only identify things.

Ask yourself: "Does adding or averaging these values make sense?" If the answer is no, the variable is not numerical.

How do you check variable types in Python?

import pandas as pd

orders = pd.DataFrame({
    "order_id": ["S00004", "S00010", "S00011", "S00014", "S00021"],
    "city": ["Port Alder", "Kingsbridge", "Northvale", "Saint Clair", "Northvale"],
    "category": ["Books", "Books", "Clothing", "Beauty", "Electronics"],
    "quantity": [1, 2, 1, 1, 1],
    "amount": [15.95, 12.90, 14.95, 21.45, 199.95],
    "satisfaction": [4, 3, 3, 5, 3],
})

print(orders.dtypes)
print(orders["amount"].mean())
print(orders["city"].value_counts())
Outputorder_id         str
city             str
category         str
quantity       int64
amount       float64
satisfaction   int64
dtype: object
53.04
city
Northvale      2
Port Alder     1
Kingsbridge    1
Saint Clair    1
Name: count, dtype: int64

dtypes shows how pandas stores each column. Text appears as str (older versions of pandas show object), whole numbers as int64, and decimals as float64. Pandas only looks at the form of the values. It stores satisfaction as a plain integer, and it is your job to know that it is an ordered category.

The mean amount is $53.04. City is categorical, so you summarize it by counting: Northvale has 2 orders, and the other cities have 1 each.

Look closer at the amounts. Four of the five orders are below $53.04, because the single $199.95 order pulls the mean up. Check it with the median:

print(orders["amount"].median())
print(orders["satisfaction"].mean(), orders["satisfaction"].median())

The median amount is $15.95, much lower than the mean. For the ordinal satisfaction scores, the mean is 3.6 and the median is 3. The median is the safer summary for ordinal data, and the mean needs careful reading.

Why does the variable type matter?

The type decides each step of an analysis:

  • Summary: mean and standard deviation for numerical variables, counts and shares for categorical ones.
  • Chart: a histogram or box plot for numerical variables, a bar chart for categorical ones.
  • Test: a t-test to compare the means of two groups, a chi-square test for the link between two categorical variables.

When you open a new table, ask one question for each column before you calculate anything: "What does this column measure, and what type is it?"

What mistakes do people make with variable types?

  • Using ID numbers as numbers. Check what happens:
print(orders["order_id"].str[1:].astype(int).mean())

The result is 12.0, a number with no meaning at all.

  • Coding categories as numbers and averaging them. Setting Port Alder = 1 and Northvale = 2 and then reporting an "average city" is meaningless.
  • Treating ordinal data as equally spaced. A satisfaction average is not as solid as an amount average.
  • Letting pandas decide the type. Pandas does not know what your columns mean.

What to read next

  • [Data table and variable types](/learn/statistics-data-analysis/data-table-variable-types): classify every column of the Bazaar data and pick a summary for it.
  • [What is data analysis?](/learn/statistics-data-analysis/what-is-data-analysis): the start of the course.
  • [Mean, median, and mode](/blog/mean-median-mode): which summary fits which type.
  • [Statistics and Data Analysis course](/courses/statistics-data-analysis): 44 free lessons.
  • #beginner
  • #data
  • #variable types
  • #statistics
All posts