Chapter 4 — First Look at the Data: Inspection, Statistics, and Charts

The First Ten Minutes with Unfamiliar Data

16 min

A Case with No Description

The year is 2184. At Vault-9 station, QUERY places a newly recovered storage device from L.’s archive on the workbench.

QUERY: No label. No documentation. The person who exported it is gone too. We have a table and a question from management. What do you do first? I’ve tested the alternatives. Repeatedly.

This is one of the most common situations in an analyst’s job, yet textbooks rarely show it. You get a file and a question. There is no description of the table structure, no explanation of the columns, and no one around who remembers what was being counted. That is no reason to start writing groupby right away. A metric calculated from a table you don’t understand may look polished, complete with a number, a unit, and a chart, while still being meaningless.

For the first ten minutes, an analyst doesn’t calculate. They inspect.

What Does One Row Represent?

The main inspection question sounds almost absurdly simple: what does one row in this table represent? The answer is called the table’s , or grain.

  • one row = one order?
  • one row = one item within an order?
  • one row = one shipment for an order that might be delivered in two boxes?
  • one row = one day and one city, meaning the data has already been aggregated by day and city rather than showing individual events?

Everything depends on the answer. If one row represents a shipment, then len(df) is the number of shipments, not the number of orders. Count rows as orders, and you’ll overstate the result by the average number of shipments per order.

L., entry No. 14: I spent three hours proving to the product manager that the table had forty thousand records. He said there were thirty-one thousand orders. Both numbers were correct, but they answered different questions: I was counting shipment rows, while he was counting unique orders. Ever since then, my first question about any table has been about what one row represents.

A cadet examines a holographic table made of cargo capsules, showing the structure of a row, a composite key, a duplicate, and a missing value.
Getting to know a table starts with two questions: what does one row represent, and which columns make it unique?

The Key

The second question is: which column or set of columns uniquely identifies a row? That is the key — a value or combination of values that distinguishes one row from every other row.

df['shipment_id'].is_unique     # True/False — can this column serve as a key?
df['shipment_id'].nunique()     # how many distinct values
len(df)                          # how many rows in total

is_unique tells you whether values repeat in a column. But uniqueness alone is not enough for a reliable key: the column must also have no missing values. For example, if a column contains one missing value, is_unique may still return True because that missing value does not repeat.

By default, nunique() ignores missing values. So if its result is smaller than len(df), the column contains repeated values, missing values, or both. You cannot treat that column as a key without checking further. For a set of columns, the same principle applies: the combination of values must be unique, even if the individual columns are not.

The cause may be harmless, such as one chunk of an export being appended twice, or more serious: the same order or shipment may appear several times with different data. Either way, you will need to investigate. First, though, you have to notice the problem.

what one row meansorders1 row = orderorder_idtotal1015 4001029001031 200order #1011 roworder_items1 row = line itemorder_iditem101tea101coffee101cakeorder #1013 rowspayment_attempts1 row = payment tryorder_idstatus101fail101ok102okorder #1012 rowsthe same order #101 — 1, 3 or 2 rowsCOUNT(*) counts a different thing in each table
Order No. 101 appears a different number of times in different tables: once in orders, three times in order_items (one row per item), and twice in payment_attempts (one row per payment attempt). So the same COUNT(*) answers different questions here: how many orders, items, or payment attempts there are.

The First-Ten-Minutes Checklist

The sequence below is reliable and takes less time than a single argument about numbers on a call.

1. Size. df.shape returns a pair: (rows, columns). A hundred rows and ten million rows require different approaches and different tools.

2. Column names. df.columns shows the column names. Read them carefully and question what they mean: what exactly is amount the amount of? Does it include shipping? Is it in dollars or cents? Half of analysis mistakes start here, when someone assumes a column name is self-explanatory.

3. Types. df.dtypes shows what pandas thinks each column contains. A number that pandas has read as text is a common archive landmine. A later chapter on cleaning will cover this in detail, but you need to spot it now.

4. Look at the data. These three views answer different questions, and you need all of them:

CommandWhat it showsWhy
df.head()the first rowssee what the data looks like
df.tail()the last rowscheck the end of the export for a “TOTAL” row, truncated data, or a run of empty values
df.sample(5)random rowslook beyond the beginning of the table; this is a quick check, not proof that the sample is typical

5. One-command summary. df.info() prints several things at once: the number of rows, the columns, the number of non-null values in each column, the types, and memory usage. This covers steps 1–3 and also helps with step 6: the number of missing values is the number of rows minus the number of non-null values.

6. Missing values. df.isna().sum() tells you how many values pandas considers missing in each column. An empty string '' usually does not count as missing. Many pandas operations, including calculating the mean, skip missing values by default, which can change both the denominator and the meaning of the result.

7. Duplicates. df.duplicated().sum() tells you how many rows exactly repeat an earlier row. With no arguments, the method compares the entire row. You can narrow the comparison: df.duplicated(subset=['order_id']) marks rows whose order_id has appeared before.

8. Categories. df['status'].value_counts() shows which non-missing values occur and how often. By default, the method ignores missing values. This is where you may discover delivered and Delivered recorded as two different statuses, along with empty strings or codes that appear in no documentation.

9. Ranges. df['amount'].min() and .max() give you the boundaries. A negative order amount, a weight of 0 kg, or a date from 2073 can all be spotted in seconds.

The chapter’s main rule is worth putting above your desk:

Don’t calculate a metric until you understand the table.

First 10 minutes with an unknown tabletable sizedf.shapewhat columnsdf.columnscolumn typesdf.dtypesfirst rowsdf.head()missing valuesdf.isna().sum()numeric summarydf.describe()categoriesvalue_counts()duplicatesdf.duplicated().sum()do not compute a metric before you know the table
Inspection checklist: size → columns → types → look at the data → info() summary → missing values → duplicates → categories → ranges. Ten minutes that can save you a day.
The storage device from L.’s archive has been unpacked into the shipments variable. We do not yet know what kind of table it contains. Run the full checklist and read the inspection report.
python · pandas

What the Table Told Us

  • 246 rows and 6 columns. For an initial analysis, this is small enough to process in full.
  • 240 unique shipment_id values across 246 rows. The column we expected to be a key is not a key. The reason appears nearby: duplicated().sum() = 6 — six rows exactly repeat earlier rows. Those six duplicates explain the difference between the total number of rows and the number of unique shipment_id values. This looks like an error introduced while combining exports.
  • 121 unique order_id values. After removing exact duplicate rows, the table will contain 240 shipments belonging to 121 orders: one order can consist of several shipments. If you answered “How many orders were there?” with 246, your result would be more than twice too high. That is exactly the kind of mistake behind L.’s three-hour argument with the product team.
  • 29 missing values in courier. Before calculating the share of shipments handled by Ginger, you need to choose the denominator: all shipments, or only those with a known courier. These are two different metrics and may lead to different conclusions.
  • status: delivered 205, in_transit 23, returned 18. These frequencies are calculated across all 246 rows in the original export, including duplicates. The column itself contains only three consistently recorded values, with no capitalization variants and no missing values — a rare bit of luck.
  • Weight ranges from 0.31 to 11.49 kg. There are no obviously impossible values: no zeros and no negatives. That does not prove every weight is correct, but the range looks plausible.

A Common Mistake

Skip the inspection and go straight to a polished groupby. If you overlook the six duplicates, row counts or grouped sums may be inflated. In a report, the wrong number can look just as convincing as the right one.

QUERY: Data doesn’t announce its problems. It simply answers whatever question you ask, even when you ask the wrong one. I’ve tested this. Repeatedly.

Practice: write the code
Turn the inspection report into numbers. Using the shipments table, calculate: n_rows — the number of rows, n_cols — the number of columns, n_missing_courier — the number of missing values in the courier column, n_dup_rows — the number of rows that exactly repeat an earlier row.
python · pandas
Practice: write the code
Now check the grain and the key. Determine: id_is_key — whether shipment_id works as a key (True/False; calculate it rather than guessing), n_orders — how many DISTINCT orders are in the table, top_status — the most frequent status, heaviest — the maximum shipment weight.
python · pandas
Check yourself
The shipments table has 246 rows, 240 unique shipment_id values, and 121 unique order_id values. Management asks, “How many orders did we process?” Which number should you give them?
Key takeaways
  • Inspect an unfamiliar table before calculating metrics: ten minutes of inspection is cheaper than a day of arguments.
  • Ask two main questions: what does one row represent — that is the table’s — and which column could be a key. Check a candidate key for uniqueness and missing values with is_unique, nunique(), and isna().
  • Checklist: shape → columns → dtypes → head/tail/sample → info() → isna().sum() → duplicated().sum() → value_counts() → min()/max().
  • Missing values force you to choose the denominator explicitly, while duplicates can inflate sums and row counts. A report with these errors can still look completely normal.
  • Rule: don’t calculate a metric until you understand the table.

Next comes the second half of the inspection: numbers that describe a column at a glance. Mean, median, quantiles, and spread.