Statistics Data
Statistics always starts with data: recorded facts about something you care about. A thermometer reading, a customer's answer to a survey question and the number of goals in a match are all data.
Observations and variables
Most datasets are organised as a table:
- Each row is one observation (also called a case, record or unit): one person, one day, one product.
- Each column is one variable: a property measured for every observation, such as age or price.
- Each cell is a single value: the value of one variable for one observation.
Here is a small invented dataset about members of a hiking club and their last hike:
| member | age | trail | distance_km | finished |
|---|---|---|---|---|
| Ana | 34 | Ridge | 12.5 | yes |
| Bo | 27 | Lake | 8.0 | yes |
| Chen | 45 | Ridge | 12.5 | no |
| Dara | 52 | Forest | 6.2 | yes |
| Eli | 19 | Lake | 8.0 | yes |
| Femi | 38 | Forest | 6.2 | yes |
There are 6 observations (members) and 5 variables. The variable trail has three possible values, finished has two, and age can take many values.
Data in Python
In plain Python a dataset can be a list of dictionaries, one per observation. Pulling out a single variable gives you a list you can summarise:
hikes = [
{"member": "Ana", "age": 34, "trail": "Ridge", "distance_km": 12.5, "finished": True},
{"member": "Bo", "age": 27, "trail": "Lake", "distance_km": 8.0, "finished": True},
{"member": "Chen", "age": 45, "trail": "Ridge", "distance_km": 12.5, "finished": False},
{"member": "Dara", "age": 52, "trail": "Forest", "distance_km": 6.2, "finished": True},
{"member": "Eli", "age": 19, "trail": "Lake", "distance_km": 8.0, "finished": True},
{"member": "Femi", "age": 38, "trail": "Forest", "distance_km": 6.2, "finished": True},
]
print("observations:", len(hikes))
print("variables:", list(hikes[0].keys()))
ages = [h["age"] for h in hikes]
print("ages:", ages)
print("finished:", sum(h["finished"] for h in hikes), "of", len(hikes))observations: 6
variables: ['member', 'age', 'trail', 'distance_km', 'finished']
ages: [34, 27, 45, 52, 19, 38]
finished: 5 of 6
For anything bigger, the pandas library is the standard tool. A DataFrame is exactly the rows-and-columns table described above:
import pandas as pd
df = pd.DataFrame({
"member": ["Ana", "Bo", "Chen", "Dara", "Eli", "Femi"],
"age": [34, 27, 45, 52, 19, 38],
"trail": ["Ridge", "Lake", "Ridge", "Forest", "Lake", "Forest"],
"distance_km": [12.5, 8.0, 12.5, 6.2, 8.0, 6.2],
"finished": [True, True, False, True, True, True],
})
print(df)
print()
print("shape (rows, columns):", df.shape)
print(df.dtypes) member age trail distance_km finished
0 Ana 34 Ridge 12.5 True
1 Bo 27 Lake 8.0 True
2 Chen 45 Ridge 12.5 False
3 Dara 52 Forest 6.2 True
4 Eli 19 Lake 8.0 True
5 Femi 38 Forest 6.2 True
shape (rows, columns): (6, 5)
member object
age int64
trail object
distance_km float64
finished bool
dtype: object
df.dtypes already hints at an important idea: some variables hold numbers (int64, float64), others hold labels (object for text, bool for yes/no). The kind of variable decides which statistics make sense, which you will study in Data Types.
Raw data versus summarised data
Raw data is every individual value as it was recorded. Summarised (or aggregated) data is what you get after counting, averaging or grouping. Summaries are easier to read but hide detail, and you can always go from raw to summary, never back.
import pandas as pd
df = pd.DataFrame({
"trail": ["Ridge", "Lake", "Ridge", "Forest", "Lake", "Forest"],
"distance_km": [12.5, 8.0, 12.5, 6.2, 8.0, 6.2],
"finished": [True, True, False, True, True, True],
})
summary = df.groupby("trail").agg(hikers=("finished", "size"),
finished=("finished", "sum"))
print(summary) hikers finished
trail
Forest 2 2
Lake 2 2
Ridge 2 1
From the summary we know the Ridge trail had one hiker who did not finish, but we can no longer tell who it was.
Where does data come from?
- Measurements: sensors, scales, timers, lab instruments.
- Surveys and questionnaires: people answering questions.
- Records: sales logs, website clicks, hospital files, public registers.
- Experiments: data you create by deliberately changing something and observing the effect.
Whatever the source, good data is accurate (values are correct), complete (few missing values), consistent (the same thing is always recorded the same way, e.g. not "yes", "Yes" and "Y") and relevant to your question. The next lessons look at how to gather and describe it.
Test yourself
In a table of 200 customers with columns age, city and spent, what is a *variable*?
- One customer
- One column such as
city - One cell
- The whole table
A DataFrame has shape (150, 4). What does that mean?
- 150 variables and 4 observations
- 150 observations and 4 variables
- 150 missing values
- 4 tables of 150 rows
Why can't you rebuild raw data from a summary table of averages?
- Summaries are always wrong
- Summarising throws away the individual values
- pandas forbids it
- Averages are text, not numbers