What should I check in a table before cleaning it? Build a problem inventory
Review the full sales sheet with five looks that change nothing, and write the list of what needs fixing before you fix anything.
Free course. Your Maestro, an AI, .
Before you fix anything, make the complete list of what's wrong. It sounds slow, and it's exactly what saves you time: if you fix the first thing you see, you'll discover the next problem after the report has already gone out. And sometimes one fix hides another problem: if you fill the blanks with zero before counting them, you no longer know how many were missing.
Looking changes nothing. None of the lines on this page modifies the table; they only ask it questions. That's why you can run them as many times as you like, in any order, without fear.
In pandas, the full March sheet is a DataFrame called ventas here. These are the five looks, each with the line that does it:
Size and types
ventas.shapetells you how many rows and columns there are, andventas.dtypestells you the type of each column. A money column of typestris text, even if it looks like a number.Blanks per column
ventas.isna().sum()counts the empty cells in each column. A blank is not an error yet: you still have to find out what it means.Values in the text columns
ventas["ciudad"].value_counts(dropna=False)lists each value with how many times it appears, blanks included. That's where the variants, the stray spaces and the abbreviations show up.Minimum and maximum of the numbers
ventas["kilos"].describe()gives the count, the minimum, the maximum and the quartiles. A negative minimum or a huge maximum is the first clue to an impossible value.What should be unique
A
folio(the sale's ID number) identifies one sale.ventas["folio"].duplicated().sum()counts how many times a folio shows up one time too many.
Here's how the inventory of the March sheet came out. We ran these lines in the Maestro sandbox, on all 60 rows:
ventas = data
print(ventas.shape)
print(ventas.dtypes)
print(ventas.isna().sum())
print(ventas["kilos"].describe())
print(ventas["folio"].duplicated().sum())(60, 9)
captura int64
folio str
fecha str
cliente str
ciudad str
producto str
kilos float64
monto str
descuento float64
dtype: object
captura 0
folio 0
fecha 0
cliente 0
ciudad 0
producto 0
kilos 1
monto 1
descuento 44
dtype: int64
count 59.000000
mean 252.949153
std 1292.906435
min -12.000000
25% 28.000000
50% 53.000000
75% 159.000000
max 9999.000000
Name: kilos, dtype: float64
2Read it as a to-do list, from top to bottom:
- 60 rows and 9 columns. This is the number you'll compare against after every cleaning step.
montoisstr. The amounts are text: you can't add them up yet. You fix that in module 2.- Blanks: 1 in
kilos, 1 inmontoand 44 indescuento. Before filling or deleting any of them, you have to know what each one means (the next page). kilosgoes from -12 to 9999. The median is 53, so those two extremes look like nothing else in the sheet. The mean, 252.9, is already contaminated by them.- 2 extra folios. Some sales appear twice, or something that looks like it.
Notice what you did not do: you didn't delete the -12, you didn't fill the blanks, you didn't remove the repeated folios. You only noted them. The complete list lets you decide calmly and ask everything at once, instead of sending Marisol five separate messages.
And notice what this inventory doesn't see: the 12 ways of writing 4 cities only show up when you count the values of ciudad, which is what you'll do.
Try it
Complete the inventory with the look that's missing. If an output isn't clear, ask your Maestro to read it to you line by line.
Add the looks for the text columns: valuecounts(dropna=False) for ciudad and for producto, and for descuento too. Then, in the comments at the end, write your list of problems, one per line. You're done when you can see the 12 ways of writing the city and your list has at least s
Add the looks for the text columns: value_counts(dropna=False) for ciudad and for producto, and for descuento too. Then, in the comments at the end, write your list of problems, one per line. You're done when you can see the 12 ways of writing the city and your list has at least six problems.
Run your code to see what it prints and the value of its last expression. Every run starts fresh. Ctrl+Enter runs it. Tab indents; to leave the editor, press Escape, then Tab.
Ready to run your code.
Run your code to see its output here.
Data: ventas-cooperativa, 60 rows
Enter saves a cell and Escape leaves it as it was. Leave a cell empty for a missing value.
| Row | Remove | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| 1 | ||||||||||
| 2 | ||||||||||
| 3 | ||||||||||
| 4 | ||||||||||
| 5 | ||||||||||
| 6 | ||||||||||
| 7 | ||||||||||
| 8 | ||||||||||
| 9 | ||||||||||
| 10 | ||||||||||
| 11 | ||||||||||
| 12 | ||||||||||
| 13 | ||||||||||
| 14 | ||||||||||
| 15 | ||||||||||
| 16 | ||||||||||
| 17 | ||||||||||
| 18 | ||||||||||
| 19 | ||||||||||
| 20 |
The blanks isna() doesn't count
isna() counts only what pandas already recognizes as empty. When it opens a CSV, pandas reads as empty the cell with nothing in it and also text like "N/A" or "NULL" (the read_csv documentation says which ones). But a lone space, a dash or a "no data" are still text: to pandas they're just one more value.
We tried it in the sandbox with a column of five cells entered five different ways: one empty, one with "N/A", one with a space, one with a dash and one with a 5. isna().sum() counted 2 blanks, and the whole column stayed as text (str) instead of becoming numbers. The other two "blanks" only showed up in value_counts(dropna=False), as a space and a dash.
That's why the inventory includes value_counts even when a column looks numeric. If a column of numbers comes out as str, or value_counts shows a "-", there are disguised blanks that isna() didn't see. Note them like the rest and ask what they mean.
Does your sheet use a dash, "n/a" or "no data" to say something is missing? Tell your Maestro how it shows up.
What to do with the list
You don't resolve the whole list yourself. Split it into two columns: what you can decide on your own (that "puebla" is Puebla, that the space in "cdmx " shouldn't be there) and what needs the person who knows the data (what an empty discount means, what happened with the -12 kilos, whether "Oaxaca" is the capital). That second column is the most important part of the inventory, and the next page is about it.
If you can't tell whether a problem on your list is yours to decide or someone else's, ask your Maestro.