Module 1 · Technical track (optional): machine learning fundamentals

Data before the model: read and split well

Lesson 2 of 15 9 min lesson Updated on October 30, 2026
Progress in the course13%

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.

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 mistakeWhy it ruins things
Standardizing the whole table before splittingThe test average gets into training
Running dropna() without counting what disappearsIt can delete a whole group
Mapping "M" to "Male" without checkingSpellings 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

  1. Download a public table, such as one from IBGE or from the dados.gov.br portal, and write the sheet with the five questions.
  2. Read the file declaring the separator and the missing values. Compare df.dtypes with what you expected.
  3. Run df.isna().sum() and write down the decision you would make for each column with missing values.
Deliverable: a file called "Dataset sheet", with the five answers, the types and the count of missing values.

Summary

Sources

Read on October 30, 2026.

  1. scikit-learn. Common pitfalls and recommended practices: data leakage and the example with a random target.
  2. pandas. IO tools: reading CSV, types and missing values.
  3. Brazil. Lei 13.709/2018 (LGPD): personal data and necessity.
  4. Gebru, T. et al. Datasheets for Datasets: the dataset sheet.

Lesson quiz

Three situations to apply what you learned.

Lesson 2 of 15

Before moving on to the next lesson

Put what you saw into practice. The boxes stay checked in this browser, so you can come back later.

Have a question about this lesson? The IV Help team answers personally on WhatsApp.

Ask on WhatsApp