跳到正文

第 4 课 · Profiling Data

Profiling a Dataset

Profile a new dataset before cleaning: row counts, missing and unique values, ranges, frequent values, text patterns and cross-column checks in Python.

Profiling means measuring a dataset before you change it. You are not fixing anything yet. You are building a list of facts and suspicions: which columns are empty, which have odd values and which do not match what you were told.

Skip this step and you will clean only the problems you happen to notice.

Start with the table

Answer these first:

  • How many rows and columns are there? Does that match your expectation?
  • What does one row stand for? An order, an order line, a customer, a day?
  • Which column or columns should identify a row?
  • What period and which places does the data cover?

The second question is the one people often skip. If you think each row is an order but it is really an order line, every order count will be wrong.

Profile each column

For each column, collect the number of empty values, the number of distinct values, the most frequent values and, for numbers, the minimum and maximum.

Python Build a small column profiler

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.

Read the output like a detective. order_id has 4 distinct values in 5 rows, so something repeats. The quantity ranges from -1 to 999. Countries are spelled in several ways. One email is empty.

These are suspicions, not verdicts. A quantity of -1 might be a return. A quantity of 999 might be a placeholder or a bulk order. Ask.

Look for patterns

For codes, phone numbers and dates, the shape of a value can tell you more than the value. Replace each digit with 9 and each letter with A, then count the shapes.

Python Find the shapes in a text column

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.

Rare shapes are a good place to look for errors, so view examples of every one of them.

Check relationships between columns

A value can look fine alone and be wrong next to another:

  • A delivery date before the order date.
  • A total that differs from quantity times unit price.
  • A city that is not in the stated country.
  • An "active" account with a closing date.

Also check relationships between tables, such as orders whose customer ID does not exist.

Profiling with pandas

pandas gives you quick summaries. Each line below answers a different question.

Python pandas: quick profile

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.

For a numeric column, describe shows a count, mean, spread and quartiles. Compare the mean with the median (the 50% line). A big gap suggests a skewed column or extreme values.

Write an issue list

Turn findings into a table you can work through:

Column Finding Possible cause Question for the owner
order_id Repeats Double load Can one order have two rows?
qty -1 and 999 Return or placeholder What do these mean?
country Mixed spellings Free-text entry Is there an official list?

Charts help too. A histogram can reveal spikes at round numbers or default values. Exploratory data analysis goes further.

Common mistake: profiling only the first few rows. Files are often sorted by date or source, so problems may start far down. Profile the whole table, or a random sample if it is huge, and run key checks on everything.

Read your numbers with care

A minimum and a maximum are the fastest way to find nonsense, but they describe only two records. Add a few more views:

  • The most frequent values. A single value that appears far more often than you expect may be a default. If a third of all birth dates are 1 January, ask why.
  • The rarest values. Values that appear once in a category column are often typing mistakes.
  • Lengths. The shortest and longest text in a column can reveal truncated names, pasted paragraphs or two values glued together.
  • Whitespace. Count values where value != value.strip().

Profile text columns before and after a simple normalization such as trimming and casefolding. If the number of distinct values falls sharply, you have found a family of spelling variants.

Profile over time and by source

A single profile of the whole table can hide a local problem. Group the same measures by month, by branch or by file name. You might find that a column is complete until June and half empty afterward, or that one source uses a different date shape. Changes like these usually have a cause you can name once you ask.

Keep the profile

Save the profile with the date and the file it describes. When a new delivery arrives, run the same code and compare. New categories, new shapes and a jump in empty values are early warnings. Many of your validation rules will start as observations from a profile.

Be careful when you share profile output. Lists of frequent values and examples can expose personal data, such as names or email addresses. Show only what the reader needs, and mask the rest.

How much is enough?

Match the effort to the risk. For a quick chart, a short profile of the columns you use may be enough. For data that drives payments or decisions about people, profile every column you touch and have someone else read your findings.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. What should you learn early about a new table?

    The meaning of one row decides how to count, join and detect duplicates.
  2. 2. A column has 4 distinct IDs in 5 rows, and IDs should be unique. What does this show?

    It proves a repeat exists, but not why. Look at the rows before removing anything.
  3. 3. What is shape profiling good for?

    Mapping characters to a shape groups formats so unusual ones stand out.

Summary

  • Profile before you change anything.
  • Learn what one row means and what should identify it.
  • Check emptiness, uniqueness, frequencies, ranges and shapes.
  • Compare columns and tables with each other.
  • Record findings as questions, not conclusions.

Next: Data Quality Dimensions

你的学习进度保存在此浏览器中,无需账号。