Exploratory Data Analysis (EDA) is the first deep look at a dataset: its shape, quality, distributions and relationships. Good EDA catches errors early and suggests which features and models to use.
What You Will Learn
- A repeatable EDA checklist
- Check shape, types and missing values
- Summarise numeric and categorical columns
- Compare groups and correlations
- Spot problems such as outliers
Prerequisites
Create the Dataset
Run this once to create emp.csv, a small employee dataset with a few deliberate problems.
import pandas as pd, numpy as np
rng = np.random.default_rng(42)
n = 200
df = pd.DataFrame({"age": rng.integers(21, 60, n),
"city": rng.choice(["Ludhiana", "Delhi", "Mumbai"], n, p=[.3, .4, .3]),
"experience": rng.integers(0, 30, n)})
df["salary"] = (25000 + df.experience * 2200 + np.where(df.city == "Mumbai", 9000, 0)
+ rng.normal(0, 6000, n)).round(-2)
df.loc[[5, 17, 40], "salary"] = np.nan # some missing values
df.loc[9, "salary"] = 900000 # a data-entry error
df.to_csv("emp.csv", index=False)
1. Shape and First Rows
import pandas as pd
df = pd.read_csv("emp.csv")
print(df.shape)
print(df.head())
(200, 4) age city experience salary 0 24 Mumbai 18 76200.0 1 51 Delhi 23 78700.0 2 46 Ludhiana 22 75100.0 3 38 Mumbai 4 34300.0 4 37 Mumbai 24 72900.0
2. Missing Values
print(df.isna().sum())
age 0 city 0 experience 0 salary 3 dtype: int64
Explanation: Three salaries are missing. Decide later whether to drop or impute them – see missing data.
3. Numeric Summary
print(df.describe().round(1))
age experience salary count 200.0 200.0 197.0 mean 40.4 15.3 65064.5 std 10.7 8.4 63077.9 min 21.0 0.0 15000.0 25% 31.8 8.0 47100.0 50% 40.0 16.0 60800.0 75% 50.0 22.0 76600.0 max 59.0 29.0 900000.0
Explanation: Red flag: the salary max is 900,000 while the 75th percentile is 76,600, and the std is about as large as the mean. One extreme value is distorting the statistics.
4. Categorical Columns
print(df.city.value_counts())
city Delhi 72 Ludhiana 68 Mumbai 60 Name: count, dtype: int64
5. Compare Groups
print(df.groupby("city").salary.median())
city Delhi 59200.0 Ludhiana 61700.0 Mumbai 62700.0 Name: salary, dtype: float64
6. Correlations
print(df[["age", "experience", "salary"]].corr().round(2))
age experience salary age 1.00 -0.03 -0.11 experience -0.03 1.00 0.37 salary -0.11 0.37 1.00
Explanation: Experience–salary correlation looks only moderate (0.37). In the outlier tutorial we remove the single bad row and it jumps to 0.93 – one error can hide the strongest relationship in the data.
7. Visualise
import matplotlib.pyplot as plt, seaborn as sns
sns.histplot(df.salary[df.salary < 200000], bins=20); plt.show()
sns.scatterplot(data=df[df.salary < 200000], x="experience", y="salary", hue="city"); plt.show()
More charts in the Seaborn tutorial.
EDA Checklist
| Step | pandas |
|---|---|
| Size and types | shape, dtypes, info() |
| Missing and duplicates | isna().sum(), duplicated().sum() |
| Numeric distributions | describe(), histograms, box plots |
| Categories | value_counts(), nunique() |
| Relationships | groupby(), corr(), scatter plots |
| Problems | outliers, impossible values, inconsistent labels |
Series
Data Science series: What is Data Science · Lifecycle · Data Cleaning · EDA · Outliers · Feature Engineering · Visualization · Seaborn · Roadmap
Interview Questions
Q: Why do EDA before modelling?
To understand data quality and structure, find errors and leakage, and choose features and models.
Q: What first signs of outliers does describe() give?
A max or min far from the quartiles and a standard deviation unusually large relative to the mean.
Practice Exercises
- Run the checklist on the Titanic dataset and write five findings.
- Check duplicated() on emp.csv and explain the result.
Summary
EDA = shape → missing → distributions → categories → groups → correlations → charts. It reveals problems like the 900,000 salary before they ruin a model.
Next: Outlier Detection
Machine Learning Tutorials, Courses and Certifications