Ir para o conteúdo

Lição 4 · Kinds of Data

Tables, Tidy Data and Common Formats

Learn to arrange data in tidy tables, link tables with keys, and recognize structured and unstructured data and common formats such as CSV and JSON.

The way data is arranged decides how easy it is to use. A spreadsheet made to look nice for a person is often hard to analyze, while a plain table that looks dull can answer many questions. In this lesson you learn what a well-shaped table looks like, how tables connect, and how to recognize the formats you are likely to receive.

Structured, semi-structured and unstructured

Three shapes of data

Structured Fixed rows and columns
Semi-structured Labels, flexible shape
Unstructured Text, images, audio
1 / 2
  • Structured data fits a fixed table, such as a database of orders.
  • Semi-structured data has labels but a flexible shape, such as a JSON file in which some records have extra fields.
  • Unstructured data has no ready-made fields, such as recordings, photos and free-form documents.

Tidy tables

A popular guideline called tidy data has three rules:

  1. each variable has its own column;
  2. each observation has its own row;
  3. each value has its own cell.

Here is a table that breaks the rules. The month, which is really a variable, is hidden in the column names.

shop Jan Feb Mar
North 120 135 150
South 90 88 101

The tidy version has one row per shop per month. Only the first rows are shown.

shop month sales
North 2026-01 120
North 2026-02 135
South 2026-01 90

The wide version is nice for a printed report. The long, tidy version is easier to filter, total and chart, and adding April means adding rows, not changing the structure. The code below converts one shape into the other.

Python From wide to tidy

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.

Habits that make spreadsheets hard to use

  • Merged cells and decorative blank rows break sorting and filtering.
  • Color as data, such as red cells meaning "unpaid". Add a paid column instead.
  • Two facts in one cell, such as Lima, Peru (new). Split them.
  • Mixed units in one column, such as 5 kg and 12 lb. Use one unit and put it in the column name, like weight_kg.
  • Totals mixed into the data rows, which cause double counting.

Keys connect tables

Repeating a customer's full address on every order invites mistakes. Instead, store customers once and refer to them by an ID. A primary key is a column (or set of columns) that identifies each row uniquely. A foreign key is a column that points to a key in another table. Joining the tables combines the information. Databases use SQL for this; the SQL tutorial goes much further.

SQL Join two small tables with SQL

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

Order 4 refers to a customer that does not exist. A plain join silently drops it, so the totals by city do not include its 10. The second query finds such orphan rows. Whenever you join data, compare row counts and totals before and after.

Common formats

Format What it is Watch out for
CSV Plain text, values split by commas No data types; separators and encodings vary
Spreadsheet file Sheets with cells and formulas Hidden rows, formatting that looks like data
JSON Nested text with names and values Records may have different fields
Database Tables with rules and queries Needs access rights and some SQL
PDF A page layout for reading Copying tables out can corrupt them

In a CSV file, a value that contains a comma must be quoted. Some countries use a comma as the decimal mark, so their CSV files often use semicolons between values. If a file opens as one jumbled column, check the separator. If accented letters look strange, check the text encoding; UTF-8 is the usual choice today.

Missing values

Tables often contain gaps. People write them as an empty cell, NA, unknown, -1 or 999. Placeholder numbers are risky: if 999 means "unknown age", it will pull an average far upward. Use one clear convention, document it in the data dictionary and check for placeholders before calculating.

A checklist for a file you receive

When someone sends you a spreadsheet or export, spend a few minutes on these checks before you build anything:

  1. Open it without changing it and save a separate working copy.
  2. Count the rows and compare with what the sender expects.
  3. Read the headers. Is each column one variable with a clear unit?
  4. Look for hidden content, such as filtered rows, extra sheets or notes below the data.
  5. Check IDs. Are they unique where they should be? Did leading zeros survive?
  6. Check dates. Are they all in the same order of day, month and year?
  7. Search for placeholders such as 0, 999 or "n/a" that mean missing.
  8. Total one important column and compare it with a trusted figure.

If a check fails, ask the sender rather than guessing. A short question now is cheaper than a wrong report later. Write down what you found and what you changed, so that you can repeat the same steps when the next file arrives.

Keep an untouched copy of the original file. Make changes in a new copy or in steps that you can repeat. If you edit the only original by hand and something goes wrong, you may never find out what changed.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. A table has columns shop, Jan, Feb and Mar holding sales. What would make it tidy?

    Month is a variable, so it belongs in its own column with one observation per row.
  2. 2. What is the purpose of a primary key?

    A unique identifier lets other tables refer to exactly one row.
  3. 3. A column of ages contains several 999 values. What should you do first?

    Placeholders must be recognized as missing before any calculation.

Summary

  • Tidy tables have one variable per column and one observation per row.
  • Avoid merged cells, color-as-data, mixed units and totals inside data.
  • Keys link tables; check for rows that a join drops or duplicates.
  • Know the weaknesses of CSV, spreadsheets, JSON and PDF.
  • Handle missing values deliberately and keep the original file.

Next: Where Data Comes From

Seu progresso fica salvo neste navegador. Não precisa de conta.