Data Types
Before choosing a chart or a formula, decide what type of data each variable holds. Calculating the "average favourite colour" makes no sense, and a pie chart of body temperatures is useless. The data type tells you which tools fit.
Qualitative vs. quantitative
| Type | Also called | Values are | Examples |
|---|---|---|---|
| Qualitative | categorical | labels or categories | blood type, country, yes/no, T-shirt size |
| Quantitative | numerical | numbers that measure or count something | height, price, number of children |
A quick test: does arithmetic on the values make sense? The average of 3, 4 and 5 children (4 children) is meaningful. The "average" of postal codes 10115, 20095 and 80331 is not: postal codes are numbers used as labels, so they are qualitative.
Discrete vs. continuous
Quantitative data comes in two kinds:
- Discrete data can only take separate, countable values, usually whole numbers from counting: goals scored, cars in a car park, defects per batch.
- Continuous data can take any value in a range, limited only by the precision of the measurement: weight, time, temperature, distance.
Money is technically discrete (whole cents), but with many possible values it is usually treated as continuous.
Data types in pandas
pandas stores each column with a dtype. It is a helpful hint about the statistical type, though not a perfect one:
import pandas as pd
pets = pd.DataFrame({
"species": ["cat", "dog", "dog", "rabbit", "cat", "dog"],
"vaccinated": [True, True, False, True, True, False],
"visits": [2, 5, 1, 0, 3, 4],
"weight_kg": [4.1, 22.5, 8.3, 1.9, 5.0, 30.2],
"zip_code": [10115, 20095, 10115, 80331, 20095, 10115],
})
print(pets.dtypes)species object
vaccinated bool
visits int64
weight_kg float64
zip_code int64
dtype: object
zip_code was read as an integer, but it is categorical. Tell pandas so, and it will stop offering nonsense like a mean:
import pandas as pd
pets = pd.DataFrame({
"species": ["cat", "dog", "dog", "rabbit", "cat", "dog"],
"visits": [2, 5, 1, 0, 3, 4],
"weight_kg": [4.1, 22.5, 8.3, 1.9, 5.0, 30.2],
"zip_code": [10115, 20095, 10115, 80331, 20095, 10115],
})
pets["zip_code"] = pets["zip_code"].astype("category")
pets["species"] = pets["species"].astype("category")
print(pets.describe()) # numeric columns only
print()
print(pets["zip_code"].value_counts()) visits weight_kg
count 6.000000 6.000000
mean 2.500000 12.000000
std 1.870829 11.563736
min 0.000000 1.900000
25% 1.250000 4.325000
50% 2.500000 6.650000
75% 3.750000 18.950000
max 5.000000 30.200000
zip_code
10115 3
20095 2
80331 1
Name: count, dtype: int64
Which summaries fit which type?
| Data type | Centre | Spread | Charts |
|---|---|---|---|
| Qualitative | mode (most common category) | number of categories, proportions | bar chart, pie chart |
| Quantitative, discrete | mean, median, mode | range, IQR, standard deviation | bar chart of counts, histogram, box plot |
| Quantitative, continuous | mean, median | range, IQR, standard deviation | histogram, box plot |
import statistics
species = ["cat", "dog", "dog", "rabbit", "cat", "dog"] # qualitative
visits = [2, 5, 1, 0, 3, 4] # discrete
weight = [4.1, 22.5, 8.3, 1.9, 5.0, 30.2] # continuous
print("most common species:", statistics.mode(species))
print("share of dogs:", round(species.count("dog") / len(species), 3))
print("median visits:", statistics.median(visits))
print("mean weight:", round(statistics.mean(weight), 2), "kg")most common species: dog
share of dogs: 0.5
median visits: 2.5
mean weight: 12.0 kg
Converting between types
You can turn quantitative data into categories by binning: ages 0–17 → "child", 18–64 → "adult", 65+ → "senior". This makes tables and charts easier but throws away detail. You can never turn categories back into exact numbers.
import pandas as pd
ages = pd.Series([4, 17, 23, 35, 64, 65, 80, 12])
groups = pd.cut(ages, bins=[0, 17, 64, 120], labels=["child", "adult", "senior"])
print(pd.DataFrame({"age": ages, "group": groups})) age group
0 4 child
1 17 child
2 23 adult
3 35 adult
4 64 adult
5 65 senior
6 80 senior
7 12 child
The next lesson refines the qualitative/quantitative split into four measurement levels.
Test yourself
Which variable is *qualitative*?
- Number of siblings
- Body temperature
- Favourite music genre
- Distance to school in km
Which variable is *discrete*?
- Time to run 100 m
- Number of emails received today
- Height
- Air pressure
A column of phone numbers is stored as int64. How should you treat it statistically?
- As continuous
- As discrete counts
- As qualitative identifiers
- As ratio data