Data Science · Exploratory Data Analysis · EDA · Guided tutorial

EDA:
learn it
by doing it

By the end you will take a raw table, find its gaps, repeats and odd values, summarise and plot it, and write the pandas code that does it all.

Before you startWhat a table of data is (rows are cases, columns are variables), what a mean and a median are, and a little Python. The theory of every step is on the EDA Blueprint; here you practise it on one small dataset that grows with you.

11
steps
51 min
estimated time
✎
you need: pen and paper
1
running dataset
01

Decide when to look at the data

⏱ 3 minthe ideanot done
Explain

Before you calculate or model anything, you look at the data: you count it, summarise it and plot it. That habit is called exploratory data analysis (EDA).

Why look? A summary squeezes a whole column into one number, and very different data can share the same numbers. In Anscombe's quartet (1973) four small datasets all have mean of x 9, mean of y 7.50 and correlation r = 0.816, with the same fitted line y = 3.00 + 0.500x. Plotted, one is a straight line and another a curve:

John Tukey's 1977 book made this “look first” approach a method.

Running example. Your lecturer hands you study.csv: twelve student records with hours studied per day, sleep per night, tutorial group and exam score. You will carry it through every step.

Try it · choose one

A classmate says: “Group A and group B in study.csv have the same mean score and the same correlation between hours and score, so I can skip the plots.” What do you reply?

02

Tell numeric from categorical columns

⏱ 4 mininspectnot done
Explain

A table has one row per case (here: one student) and one column per variable. Before anything else you ask of every column: what kind of values does it hold?

A numeric variable is a measured amount: you can add it, average it, plot its shape. A categorical variable is a label or group: you count how often each label occurs. An id is written with digits but only names the row; its average means nothing.

In pandas the first look is df.shape (rows, columns), df.dtypes (the type of each column) and df.head() (the first rows). For study.csv, df.shape gives (12, 5).

Try it · click the table

Click the header of every column that holds a numeric variable you could sensibly average. Click again to unselect.

study.csv · raw file · 12 rows × 5 columns

NaN = empty cell (not a number)

03

Count the missing values

⏱ 4 mininspectnot done
Explain

An empty cell is a missing value; pandas shows it as NaN. Gaps are rarely spread evenly: usually one or two columns hold almost all of them, so you count them per column with df.isna().sum().

The share of a column that is missing is

missing cells in the column ÷ number of rows

Mini-example (Kaggle Titanic, from the Blueprint): 891 passengers, 177 without an Age → 177 ÷ 891 = 19.9 % of the Age column.

The share tells you what to do next: a small share can be filled, a column that is mostly empty is usually dropped.

Try it · type a number

In the raw study.csv below, what percentage of the sleep column is missing?

% · 1 decimal · ±0.2 accepted
04

Put the EDA routine in order

⏱ 4 mincleannot done
Explain

EDA moves through three kinds of stage: inspect what you have, clean what is broken, explore what it means. Like checking a second-hand car: look it over, fix the flat tyre, then take the test drive.

Two rules fix the order. Count before you fix: if you fill the gaps first you can no longer see how many there were. Clean before you summarise: a repeated row is counted twice in every mean.

And what you find is a set of questions, not proof: you check them later on new data (confirmatory analysis).

Try it · put in order

Put the six actions for study.csv in the order you do them (first at the top). Use ▲ and ▼; they work with the keyboard too.

  1. Summarise each column: df.describe() and the five-number summary
  2. Count missing cells and duplicate rows
  3. Write down what you found as questions to confirm on new data
  4. Load the file and check its size and column types
  5. Plot distributions and relationships
  6. Drop the repeated row and decide, column by column, how to handle the gaps
05

Predict what one wild value does to the mean

⏱ 4 minexplorenot done
Explain

Cleaning study.csv: 12 rows − 1 duplicate = 11, − 1 row without an exam score (the column you want to explain) = 10 students. The one sleep gap left is filled later.

The mean is the sum ÷ n. The median is the middle of the sorted values (with an even n, the average of the two middle ones). One extreme value drags the mean a long way; the median barely moves. That is why EDA reports both.

Hours studied per day of the 10 students:

2, 3, 1, 4, 2.5, 40, 3.5, 5, 1.5, 4.5

Sorted, the middle two are 3 and 3.5, so the median is 3.25. Forty hours a day is impossible, but first see what it does.

Try it · predict, then reveal

Set the slider to your prediction of the mean hours of these 10 students, then press Check to reveal the true value.

0.0 h/day

Accepted within ±0.5 h. The true mean is revealed on Check, so the first try counts.

06

Your turn: find the five-number summary

⏱ 6 minexplore✎ pen and papernot done
Explain

The five-number summary is min, Q1, median, Q3, max: what a box plot draws and what df.describe() prints. The quartiles Q1 and Q3 cut off the lowest and highest quarter.

pandas finds the p-th quantile at position p · (n − 1) of the sorted list, counting positions from 0. A position between two values lies that fraction of the way between them.

Mini-example: 2, 4, 6, 8, 10 (n − 1 = 4). Q1 at 0.25 · 4 = 1 → 4; median at 2 → 6; Q3 at 3 → 8. A position of 2.25 would mean “a quarter of the way from the value at 2 to the value at 3”.

minQ1medianQ3max box = middle half (IQR)
Try it · three parts

Exam scores of the 10 cleaned students: 58, 64, 50, 71, 62, 66, 68, 77, 55, 74. Min = 50, max = 77. Find the other three numbers with the pandas rule.

points · ±0.05
points · ±0.05
points · ±0.05
07

Fence the outliers with 1.5 × IQR

⏱ 5 minexplore✎ pen and papernot done
Explain

EDA flags a value as a suspected outlier when it lies beyond Tukey's fences, built from the middle half of the data so the odd value cannot move the ruler:

IQR = Q3 − Q1
fences = Q1 − 1.5·IQR , Q3 + 1.5·IQR

A flagged value is investigated, not deleted: a typo is corrected, a real extreme is kept and reported.

The 10 hours values from step 5, sorted: 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 40. With the rule from step 6: Q1 (position 2.25) = 2.125 and Q3 (position 6.75) = 4.375.

Try it · type a number

What is the upper fence for hours studied per day? Any value above it is flagged.

h/day · ±0.01
08

Match each question to its plot

⏱ 4 minexplorenot done
Explain

Which plot you need depends on how many variables you look at and whether they are numeric or categorical (step 2).

you look atdraw
one numeric: its shapehistogram (or a box plot)
one categorical: how many of eachbar chart of counts
two numeric: do they move together?scatter plot
numeric split by a categorybox plot per group

Like a doctor who first checks each measurement on its own, then pairs such as height and weight.

Try it · match pairs

For each question about the cleaned study.csv, choose the plot to draw first.

(a) How are the 10 exam scores distributed: one peak, skewed, gaps?
(b) Does the exam score rise with hours studied?
(c) How many students are in each group A, B, C?
(d) Do the scores differ between the groups?
09

Read a correlation without fooling yourself

⏱ 4 minexplorenot done
Explain

The correlation r, between −1 and 1, measures how closely two numeric variables follow a straight line together:

r = cov(x, y) ÷ ( sₓ · s_y )

Three cautions. One extreme point can make or break r. A perfect U-shape gives r ≈ 0, so “near 0” does not mean “unrelated”. And r means “move together”, never “cause”.

Scanning many pairs also finds things by luck: 20 columns give 20 × 19 ÷ 2 = 190 pairs, and at α = 0.05 about 190 × 0.05 = 9.5 look “significant” in pure noise. EDA findings are hypotheses to confirm.

Try it · choose one

With the typo 40 still in, r(hours, score) for the 10 students is 0.17. After correcting it to 4 (step 7), r = 0.98. Which conclusion is sound?

10

Write the pandas lines

⏱ 5 minpythonnot done
Explain

pandas reads the table into a DataFrame df; seaborn draws the plots. Five DataFrame methods cover steps 3 to 6:

isna
True where a cell is missing; .sum() counts them per column
duplicated
True for a row that repeats an earlier row
dropna
drops rows with gaps; subset= limits it to some columns
median
the middle value of a column (ignores NaN)
describe
count, mean, std, min, Q1, median, Q3, max per numeric column

Filling a gap with the column median is called imputation.

Try it · fill in the blanks

Complete the EDA script for study.csv. Type the method name only.

import pandas as pd
import seaborn as sns
df = pd.read_csv("study.csv")
df.shape                          # (12, 5)
df.().sum()          # missing cells per column
df.().sum()          # repeated rows → 1
df = df.drop_duplicates()
df = df.(subset=["score"])  # no target → drop
df["sleep"] = df["sleep"].fillna(df["sleep"].())
df.()                # five-number summary + mean
sns.scatterplot(data=df, x="hours", y="score")
11

Put it all together: run EDA on a new file

⏱ 8 mincapstone✎ pen and papernot done
Explain

A second tutorial group sends lab2.csv. Run the whole routine on it: inspect (steps 2–3), clean (step 4), summarise (steps 5–6), fence (step 7), and decide what a flagged value means (steps 7 and 9).

Nothing new here: every part uses one earlier step. Work on paper, top to bottom.

lab2.csv · raw · 9 rows
Try it · four parts

Clean lab2.csv as in study.csv (drop the repeated row and the row without a score), then answer:

rows
h · ±0.05
points · ±0.05
✓

What you can do now

Each outcome is ticked when the steps that teach it are done. Your progress is saved in this browser.
    score · solved without the worked answer
    0 / 11

    review a step
    A

    Answers