Saltar al contenido

Lección 5 · First Look at Data

Data Types and the Grain of a Table

Classify variables by meaning, convert text to correct types, and find the grain and key of a table so that counts and sums are not silently inflated.

Two questions decide which operations make sense on a table. What kind of thing does each column hold? And what does one row stand for? A computer will happily average postal codes or add up the same order three times. You have to know better.

Variable types by meaning

The storage type tells you how a value is kept: integer, text, date. The measurement type tells you what the value means. They are often different.

Measurement type Meaning Example Useful summaries
Nominal Names with no order city, color counts, shares, mode
Ordinal Ordered categories small, medium, large counts, median category
Discrete numeric Counts items per order sum, mean, median, counts
Continuous numeric Measurements weight, duration mean, median, spread
Date and time Points in time signup time range, trend, gaps
Identifier Labels for things customer ID uniqueness, matching
Boolean Yes or no is_member share of yes

A column of integers can be any of several types. Store number 17 is nominal. A rating from 1 to 5 is ordinal. People often average ratings, and it can be useful, but remember that it assumes equal steps between 1, 2, 3, 4 and 5. Show the distribution of ratings next to any average.

Convert with checks

When you turn text into numbers or dates, count what failed. A silent failure becomes a missing value that you may never notice.

Python Convert text and report failures

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 three failures are different. "N/A" is a missing marker. "free" might mean zero, or something else. "2026-02-30" is an impossible date that suggests an entry or export error. The comma removal assumes that a comma is a thousands separator; in a file with decimal commas it would silently change the values.

Types in pandas

pandas can force a conversion with errors="coerce", which turns failures into missing values. Always find out which values turned into gaps.

Python Coerce, then inspect what failed

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.

An ordered categorical sorts by meaning, not alphabetically. The categoricals lesson has more detail.

The grain of a table

The grain is what exactly one row represents. Say it as a sentence: "One row is one product line within one order." Then find the key: the column or columns that should identify each row uniquely.

Same shop, different grain

Order table One row per order
Line table One row per product line
Daily table One row per day

Counting rows means something different in each

1 / 2

Test the key

Never assume that a column is unique. Test it.

Python Which columns identify a row?

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.

order_id repeats, so the key is order_id plus line. The column order_total belongs to the order, but it appears on every line. Adding it row by row counts order A1 twice.

Joins can change the grain

The same problem appears when you join tables. If one customer has two addresses and you join orders to addresses on the customer, each of that customer's orders appears twice.

SQL A join that multiplies rows

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

Check row counts and key totals before and after every join. In pandas, merge(..., validate="many_to_one") raises an error when the right side has repeated keys. See merge and join.

Do not "fix" duplicated rows from a join with a blanket DISTINCT or drop_duplicates() before you understand them. Two real purchases can look identical. Fix the join or aggregate to the right grain first.

Record a data dictionary

If you have no data dictionary, start one: column name, meaning, measurement type, unit, allowed values and open questions. Units deserve special care. A duration could be in seconds or milliseconds, and a price in cents or whole currency units.

Codes that look like numbers

Many columns use numbers as codes: status 1, 2 and 9, or payment type 0 to 4. A summary function will average them without complaint. Convert such columns to labeled categories early, using the data dictionary, so that nobody mistakes a mean status of 2.4 for a fact.

Watch for these special cases too:

  • Money. Binary floating-point numbers cannot store many decimal fractions exactly. For exact accounting, use whole cents or Python's decimal module.
  • Long identifiers. Very long digit strings can lose precision if a tool treats them as floating-point numbers. Keep them as text.
  • Percentages. A value of 0.5 might mean 50% or 0.5%. Check the range and the documentation.
  • Booleans. Text such as "no" or "false" is a non-empty string, which Python treats as true. Map the words explicitly.

Try it yourself

Write the grain of each of these tables in one sentence: a table of bank transactions, a table of daily weather by station and a table of students' course enrollments. For each one, name the columns that should form a unique key. Then think of one situation that could break that key, such as a station that reports twice after a restart.

Choosing the grain for a question

Different questions need different grains. "Which products sell most?" needs product lines. "How large is a typical order?" needs one row per order, so you first add up the lines within each order. "How many customers came back?" needs one row per customer. If you compute the typical order size from line rows, you get the typical line size, which is smaller and answers something else. Name the grain of every table you create, including temporary ones.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. A column holds store numbers 1, 2 and 3. What measurement type is it?

    The numbers are only names. Their order and average have no meaning.
  2. 2. What is the grain of a table?

    The grain tells you what you are counting when you count rows.
  3. 3. An order total repeats on every product line. What happens if you sum that column over all lines?

    Aggregate to one row per order before you sum order-level values.
  4. 4. What should you check after a join?

    A one-to-many match can multiply rows and inflate totals.

Summary

  • Storage type and measurement type are different; meaning decides which summaries fit.
  • Count conversion failures instead of letting them vanish.
  • State the grain in one sentence and test the key for uniqueness.
  • Order-level values on line-level rows and one-to-many joins inflate totals.
  • Keep a data dictionary with units and open questions.

Next: Missing Values and Data Quality

Tu progreso se guarda en este navegador. No necesitas una cuenta.