In this lesson
Preparing means deciding, not just cleaning.
Before you train a model, the table has to be right. Here you learn to read a table without ruining it and to avoid the most common mistake: giving the model information from the test set without noticing.
- Read a file declaring types and missing values
- Handle missing values and duplicates with clear criteria
- Split the data without leaking the test set
Where the data comes from
Use sources with clear rules: a public API, such as the one from IBGE, Brazil's national statistics agency, or an open data portal with a stated license. Scraping a website is something else. The robots.txt file is a request to bots, not a permission, and the terms of use override it.
If the table has data about people, the LGPD, Brazil's data protection law, applies. Name, CPF (the Brazilian taxpayer ID number) and phone number are personal data. If the model does not need the column, it goes before any other step.
Before opening the table, write a sheet with five answers. Who collected it, and what for? What is a row: a person, a purchase, a day? Who was left out? What does each column mean, and in which unit? What has already been done to it?
Reading without ruining
pandas tries to guess the type of each column. One "not provided" in the middle of numbers turns the whole column into text. So declare everything when you read.
df = pd.read_csv(
"orders.csv",
sep=";", # Brazilian spreadsheets use semicolons
decimal=",", # and commas for decimals
na_values=["not provided", "-"], # missing values pandas does not recognize
dtype={"amount_cents": "Int64"},
)
The Int64 type, with a capital I, accepts missing values without turning into a decimal. A regular integer becomes a number with ".0" as soon as a single missing value shows up.
Missing is not zero
Missing means "we do not know". Zero is a value. If you fill age with zero, the model will treat a zero-year-old baby as real data. Count before you decide: df.isna().sum() shows how many are missing in each column.
Then ask where they are missing. If the missing values are concentrated in one group, such as the orders placed through the app, deleting those rows deletes the whole group. To fill them in, use the median calculated on the training data only.
A duplicate is a decision
Are two rows "the same thing" when all the columns are equal? Or when they have the same order number? The answer changes what you delete. Define the key that should be unique, count the repetitions and look at a sample before removing anything.
One kind of duplicate is misleading: the same person in training and in test. The model recognizes the person and the score goes up without being real. Keep each person entirely on one side.
Split before anything else
Separate training and test before any calculation. The test set is the exam the model cannot see ahead of time. If the class you want to predict is rare, use a stratified split, which keeps the proportion on both sides. If the rows are ordered in time, split by date. Shuffling would make the model train on the future.
| Common mistake | Why it ruins things |
|---|---|
| Standardizing the whole table before splitting | The test average gets into training |
| Running dropna() without counting what disappears | It can delete a whole group |
| Mapping "M" to "Male" without checking | Spellings outside the dictionary vanish without warning |
| Dates with no declared format | "10/11/12" can be read three ways |
What the pros know
Leakage is treacherous because the score looks great. scikit-learn shows this with a test: with the target drawn at random, choosing the columns using all the data gave an accuracy of 0.76. Choosing with the training data only, the number fell to 0.5, which is chance. In other words, the model "got right" something impossible. Professionals put every transformation inside a Pipeline, which learns from the training data only, and run this sanity test: they shuffle the target and check that the score falls to near chance.
Practice · 5 min
- Download a public table, such as one from IBGE or from the dados.gov.br portal, and write the sheet with the five questions.
- Read the file declaring the separator and the missing values. Compare
df.dtypeswith what you expected. - Run
df.isna().sum()and write down the decision you would make for each column with missing values.
Summary
- Check the origin and the license of the data. An unnecessary personal column goes first.
- Declare separator, missing values and types when you read.
- Missing is "we do not know", not zero. Count before you decide.
- Split before any calculation and use a Pipeline.
Sources
Read on October 30, 2026.
- scikit-learn. Common pitfalls and recommended practices: data leakage and the example with a random target.
- pandas. IO tools: reading CSV, types and missing values.
- Brazil. Lei 13.709/2018 (LGPD): personal data and necessity.
- Gebru, T. et al. Datasheets for Datasets: the dataset sheet.
Lesson quiz
Three situations to apply what you learned.
Isaque Victor free courses