Sub topic 1
What a First Exploratory Analysis Is For
Prerequisites
This chapter assumes you can do the following:
- Write basic Python: variables, lists, dictionaries, and functions.
- Run code in a Jupyter notebook or a Python script.
- Use an environment where Python, pandas, and matplotlib are installed. If you are missing them,
pip install pandas matplotlibis enough.
You do not need statistics beyond knowing what an average is. This chapter has almost no code. It sets up the goals and the working method for the rest of the tutorial, and the later chapters carry out that method on the Titanic passenger list.
What exploratory analysis means
Exploratory data analysis (EDA) is the work you do on a table of data before you build a model or draw a conclusion. You look at the data to learn what it contains, what is wrong with it, and which questions it can answer.
A first pass has three outputs:
- A description of the data: its size, its columns, the type of each column, and how each column is distributed.
- A list of problems: missing values, duplicates, impossible values, and columns that are not what their names suggest.
- A short list of questions and hypotheses to test later, with caveats about how far the data can support them.
EDA is not hypothesis testing. When you explore, you may look at many columns and many comparisons, and some patterns will appear by chance. Treat what you find as leads to follow, not as established facts.
Why start with exploration
Skipping exploration is a common cause of bad analyses. The failures are usually quiet. The code runs, the numbers print, and the results are wrong.
- A column that looks numeric may be stored as text, so averages fail or sort in the wrong order.
- A column with 77% missing values may be silently dropped by a later step, changing which rows you analyze.
- Duplicated rows can double-count some people and inflate a group.
- A rate computed on four rows can look dramatic and mean nothing.
- A coded value such as
-1or999may stand for "unknown" but get averaged as a real number.
A first look catches these problems while they are cheap to fix. It also tells you whether the data can answer the question you have in mind. If you want to study survival and the file has no survival column, you want to learn that in the first five minutes.
The dataset: the Titanic passenger list
This tutorial uses the Titanic training file, train.csv. Each row is one passenger, and each column is one fact about that passenger. The columns you will meet include:
| Column | Meaning |
|---|---|
| Survived | 1 if the passenger survived, 0 if not |
| Pclass | Ticket class: 1, 2, or 3 |
| Name | Passenger name |
| Sex | `male` or `female` |
| Age | Age in years, with some values missing |
| Fare | Ticket price |
| Cabin | Cabin number, mostly missing |
| Embarked | Port of boarding, with a few values missing |
The file has real-world flaws: gaps, a column that is mostly empty, and categories that need care. That is why it suits a first exploration. A tidy toy dataset would not show you any of the problems you will meet in real tables.
The column Survived is the outcome we care about. Most of the analysis asks how survival differs across groups, such as by sex, class, and age.
The questions to ask
A useful exploration is driven by questions. Without them, you print summaries and nothing connects. Here are the questions this tutorial answers, in the order you will meet them.
- What is the structure? How many rows and columns are there, and what type is each column?
- What is missing? Which columns have gaps, how many, and might the gaps be systematic?
- Is the data clean enough? Are there duplicate rows or implausible values?
- What does each column look like? What are the ranges, centers, and spreads of numeric columns, and the categories of text columns?
- How do groups differ? Does survival differ by sex, class, or age group?
- Do variables move together? Which numeric columns correlate, and what does that fail to tell you?
- What do I now believe, and how sure am I? What are the findings, caveats, and next steps?
Each later chapter answers one or two of these with a small amount of pandas code.
The shape of the routine
The chapters follow a fixed order, because each step depends on the one before it.
The dotted arrow matters. Exploration is a loop. A plot may show a strange spike that sends you back to check for a coded value. A group comparison may reveal a tiny subgroup that makes you doubt a result. Expect to revisit earlier steps.
What you will do, and with which tools
The tutorial uses two libraries.
- pandas holds the table in a
DataFrame, which is a two-dimensional table with labeled columns, and provides methods to inspect, filter, group, and summarize it. - matplotlib draws the charts. You will call it through the pandas plotting API, so most plots take one line.
Here is a preview of the first lines you will write in the next chapters. You do not need to understand them yet. They show how small the entry cost is.
import pandas as pd
df = pd.read_csv("train.csv")
print(df.shape)
print(df.head())
df.info()These four calls report the number of rows and columns, show the first five rows, and list each column's type and non-null count. That is already enough to answer the first question above.
What a first pass cannot do
Be clear about the limits before you begin, because they shape how you write your findings.
- It cannot show cause. If first-class passengers survived more often, the data alone does not say the ticket class caused it. Class is tied to cabin location, age, and sex, among other things. The tutorial returns to this in its chapter on interpretation.
- It cannot fix biased data. The file contains the passengers it contains. If records are missing for a systematic reason, no pandas call recovers them.
- It does not give certainty. A difference between two group rates can come from small samples. You will report counts next to rates for this reason.
- It is not a model. You will not predict anything. A model comes after you understand the data well enough to choose features and handle gaps.
Stating these limits in your notes is part of the work, not an apology.
Habits that make an exploration trustworthy
A few habits separate a useful exploration from a pile of printed output.
- Keep the raw data untouched. Load the file once and never overwrite the original columns. When you need a changed version, such as age bands, add a new column. You will do this with
pd.cutand keepAgeintact. - Print counts with percentages. A rate without its denominator is easy to misread.
- Write the question before the code. One line such as "Do missing ages cluster in one class?" keeps each cell purposeful.
- Check results against what you expect. If the row count after a filter is zero or equals the full table, the filter is probably wrong.
- Make the notebook rerunnable. Run it top to bottom from a fresh start. If it only works in the order you happened to execute cells, it is not a record anyone can reproduce.
What you should be able to do after the tutorial
By the final chapter you will be able to:
- Load a CSV file into a DataFrame and inspect its shape, columns, and dtypes.
- Measure and interpret missing values and duplicate rows.
- Summarize numeric and categorical columns with
describeandvalue_counts. - Select and filter with boolean masks and
loc. - Compare subgroups with
groupbyandagg. - Create derived columns without overwriting raw data.
- Plot distributions and group comparisons.
- Compute a correlation matrix and say what it does and does not show.
- Record findings, caveats, and next questions.
The next chapter introduces the two objects behind all of this, the Series and the DataFrame.
Exercise
A colleague runs df.dropna() on the Titanic data to "clean it up" and then reports that first-class passengers survived at a higher rate. Give two reasons the first pass should have come before that step.
Show answer
First, dropna() removes every row with any missing value. Cabin is mostly empty, so this would discard most of the table, and the remaining passengers are not a typical sample. The survival rate you compute is then about a different population. Second, you do not yet know why values are missing. If missing ages or cabins are concentrated in certain classes or outcomes, dropping them biases the comparison. A first pass measures the gaps per column and looks at where they fall before deciding how to handle them.
Exercise
Name three outputs a first exploratory analysis should produce, and say which of them a model-building step depends on most directly.
Show answer
The outputs are a description of the data (size, types, distributions), a list of problems (missing values, duplicates, implausible values), and a list of questions with caveats. A model-building step depends most directly on the list of problems, because it determines how gaps and bad values must be handled, and on the description, because it guides which columns to use as features. Either answer is acceptable if you justify it from the data's issues.