Aller au contenu

Leçon 4 · First Look at Data

Loading Data and Taking a First Look

Load a CSV file safely and run first checks: row and column counts, header names, sample rows, value previews and signs that the file was read wrongly.

The first ten minutes with a new dataset can save hours later. Before you calculate anything, you want to know where the data came from, whether it loaded correctly and roughly what it contains.

This lesson gives you a repeatable first-look routine in plain Python and in pandas.

Before you open the file

Ask the person who gave you the data a few questions:

  • What produced it: an app, a sensor, a form, a manual spreadsheet?
  • When was it exported, and which period does it cover?
  • Were any rows filtered out?
  • Is there a data dictionary, a document that explains each column?
  • Does it contain personal or sensitive information, and what are the rules for it?

The answer to "Were any rows filtered out?" can change the meaning of every result.

Load without guessing

A CSV file is plain text with rows and a delimiter between values. It looks simple, but delimiters, quoting, encodings and decimal marks vary. Always read a few raw lines first.

Python Peek at raw text, then parse it

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice two things. First, the quoted value "Porto; Centro" stays in one column because a real CSV parser respects quotes; splitting each line on ; yourself would break it. Second, every value is text. The amount 12,50 uses a decimal comma and is not yet a number. See the Python CSV lesson for more.

Loading with pandas

The pandas function read_csv has parameters for these details. The example reads from a string; with a real file you pass its path.

Python read_csv with explicit options

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The code reads order_id as text on purpose. IDs are labels, not quantities, and some have leading zeros that a number type would remove. For other formats see reading other formats.

The first-look routine

After loading, answer these questions in order.

First-look routine

Size Rows and columns
Names Clear and unique?
Rows Top, bottom, random
Types Numbers, dates, text

Compare each answer with what you expected

1 / 2

In pandas, the usual tools are df.shape, df.columns, df.head(), df.tail(), df.sample(5, random_state=1), df.info() and df.describe(include="all").

Look beyond the top rows

The first rows are often unusually tidy. Problems can hide at the end of a file (a "Total" line, an export message) or in the middle. Look at the head, the tail and a random sample.

Python Head, tail and random rows

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last row is a total that was stored as data. It doubles the sum of the column. A look at the head alone would never reveal it. A small random sample would probably miss it too, which is why you check both ends.

Check the size against the source

Compare your row count with an independent number: the count shown by the source system, a database query or a figure from the data owner. A mismatch can mean a truncated export, skipped lines, duplicated files or a different time window. Be careful with suspiciously round counts such as exactly 1,000 or 10,000, because many tools limit exports.

In a database, you can get the size and coverage in one query.

SQL Row count and date coverage in SQL

Press Run (or Ctrl + Enter) to see the result here.

The range covers five calendar days, but only three of them have orders. Closed days, or missing data? That is your first question for the owner.

Common loading mistakes

Watch for these: a header row read as data, extra title lines above the header, numbers stored as text because of currency signs or thousands separators, dates read with day and month swapped, and garbled letters from the wrong text encoding. The read_csv options skiprows, thousands and encoding help with several of them. Check a few known values after loading.

A date such as 03/04/2026 can mean 3 April or March 4. Never trust a guess. Ask for the format, and then state it in code, for example pd.to_datetime(df["date"], format="%d/%m/%Y").

Describe what you see

After the routine, write a short description in plain words. For example: "The file has one row per order line, 12 columns and about 48,000 rows from January to March. Two columns are completely empty. The amount column was read as text because some values contain a currency sign."

That paragraph is useful in three ways. It forces you to notice things, it gives the data owner something concrete to confirm or correct, and it becomes the start of your report. The numbers in this example are invented.

When the file does not load

If loading fails, read the error message and the line it mentions. Typical causes are an unexpected delimiter, an unmatched quote or a different encoding. Resist options that skip bad lines until you know how many lines would be skipped and what they contain. Silent skipping can remove exactly the unusual records that matter, such as orders whose comment field contains a quote or a line break.

For a very large file, load a limited number of rows first with the nrows option of read_csv to learn the structure. Remember that the first rows are not a representative sample, so repeat your checks on the full data or on a random sample.

Try it yourself

In the first example, add a fourth line with a missing amount and a fifth with the city written inside quotes with a semicolon. Run it and check that the row count and columns stay correct. Then change the pandas example so that it fails loudly if the column names are not the four you expect.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Why should you read an order ID as text?

    Identifiers are not quantities, so keeping them as text preserves them exactly.
  2. 2. Why look at the last rows and a random sample, not only the first rows?

    The top of a file is not necessarily representative.
  3. 3. A file has exactly 10,000 rows. What should you consider?

    Compare the count with the source system to rule out truncation.
  4. 4. What does the date text 03/04/2026 mean without more information?

    Day-month and month-day conventions differ, so specify the format.

Summary

  • Learn the source, period and filters before you analyze.
  • Read raw lines, then load with explicit delimiter, decimal, date and type options.
  • Check size, names, head, tail, random rows and types.
  • Compare row counts and date coverage with an independent source.
  • Totals rows, truncated exports and ambiguous dates are classic traps.

Next: Data Types and the Grain of a Table

Votre progression est enregistrée dans ce navigateur. Aucun compte nécessaire.