…
…
Clean a student club's raw membership form, with its duplicates, different spellings of cities and departments, dates in three formats, and impossible ages and years; justify every decision and show how the summary changed before and after cleaning.
The university's Hiking Club kept its fall membership form open from September 14 to October 9, 2026. For the start-of-term report, the club president wants three numbers: how many members are there, what is the mean age, and how many different cities and departments do the members come from? The export of the form is dirty: some rows were written twice, some students filled in the form again, the same city was written in four different ways, the dates come in three formats, and a few ages and years have impossible values. In seven steps you will clean the table, justify the type of each decision, and show in one table how the summary changed before and after cleaning.
Data: membership_raw.csv (154 rows, fictional sample data), where each row is one form submission. There is no personal information except the first name.
| Column | Description |
|---|---|
record_no | Form submission number; grows with the submission order |
student_no | Fictional student number; the key that identifies each member |
name | The member's first name |
application_date | The day the form was submitted, as text: 2026-09-14, 14.09.2026, or 09/14/2026 |
age | The age the member wrote (can be empty) |
year | 1, 2, 3, or 4 (can be empty) |
department | Department, typed by hand (can be empty) |
city | Hometown, typed by hand (can be empty) |
Lessons used: data quality and profiling (18), missing values and NaN (19), duplicates, text standardization, and date formats (20), business rule, IQR rule, and the correct, remove, keep, flag decisions (21), before/after report (22). Mean and median (12). There is no new concept.
Cells share one session; each step continues with the u table that the previous step cleaned. If you want to run a step again, run the steps in order starting from step 1, because the cleaning steps change the table.
What you'll do
Required topics
membership_raw.csvDownload file
Reading file…
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
The club president asks: "How many members do we have, what is our average age, and how many different cities and departments do we come from?" You could calculate these numbers from the raw file right away, but can you trust them? First look at the table without changing it (lesson 18): how many empty cells are in each column?
Then save a summary of the raw table as before. The summarize(df) function produces the summary: the number of rows, the number of different student numbers (members), the mean and median age, the number of rows with an age (age_filled), and the number of different city and department spellings. The only thing missing from the function is the mean age. At the end you will apply the same function to the clean table and put the two summaries side by side.
Look at the summary: there are 154 rows but only 140 different student numbers. The club knows that its members come from 8 cities and 5 departments. How many cities and departments does the summary show?
This step's code cell is for members.Sign up free
There are 154 rows but 140 student numbers: some students appear in more than one row. There can be two different reasons (lesson 20):
record_noCode cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
record_no and one or two fields differ.First count and delete the exact duplicates. Then count the second forms that come with the same student number; the cell shows a few pairs side by side. Which one should stay? The club's rule: the latest submitted form is valid, because the student filled it in again to correct their information. record_no grows with the submission order and the table is in this order, so the last row of each student is the newest form.
In the end, each student should have a single row.
This step's code cell is for members.Sign up free
The first summary counted 20 cities and 13 departments; the club knows it has members from 8 cities and 5 departments. For a computer, Port Alder, PORT ALDER, Port Alder, and pt. alder are four different cities. Some values also end with an invisible zero-width space (U+200B), which usually comes from copying and pasting; look at BUSINESS in the pair table of the previous step. strip() and lower() do not remove it, but str.replace("\u200b", "") does. Apply a four-step chain to both columns:
1. strip the spaces at the start and the end,
2. remove the zero-width space,
3. lowercase,
4. match the remaining differences with a dictionary (.replace({...}), with no .str in front).
You will build the city dictionary yourself: first write the first three steps, run them, and look at the value_counts() output below; add the cities that still have two spellings to the dictionary. The department dictionary is ready. Why does it contain architecure? A typo cannot be fixed by changing case or removing spaces; only a dictionary can match it.
This decision is of the correct type: the right value is certain, because st. clair and saint clair are two spellings of the same city.
This step's code cell is for members.Sign up free
The president also wants to see the number of applications per week; for that, the text in application_date must become a date. The form has three formats: 2026-09-14, 14.09.2026, and 09/14/2026. The rule from lesson 20: if you know the format, state it. Try each format with format and errors="coerce" in turn, and fill the values that stayed empty (NaT) in the previous attempt with fillna. Note that in 09/14/2026 the month comes first. format="mixed" looks easy, but it can silently mix up days and months.
After converting, make two checks: how many dates could not be converted, and what are the earliest and latest dates? The form opened on September 14, 2026 and closed on October 9, 2026. A date outside this range breaks a consistency rule (lesson 21: business rule).
Decision: we do not know the correct value of the dates that could not be converted or that fall outside the period, but the student really applied. So we do not delete the row; we only empty the date (pd.NaT, the empty value of a date).
This step's code cell is for members.Sign up free
The age column has values like 210 and -19. Let's look at which ones are errors in two ways (lesson 21).
Compare the two lists: does the IQR rule catch everything the business rule catches, and does it flag more? According to the club records, two members aged 29 and 33 really exist: one is studying for a second degree, the other works and studies.
Decision: for ages and years that break the rules, the error is certain, but the correct value is unknown. Did the person who wrote 210 mean 21 or 20? We do not make up a value: we empty it (np.nan) and keep the member. Missing values are counted with isna(), and mean skips them (lesson 19).
This step's code cell is for members.Sign up free
Show the effect of the cleaning decisions on the summary in one table: before from step 1 and the summary of the clean table, after, side by side. If you combine the two Series with pd.DataFrame({"before": ..., "after": ...}), the rows line up by measure name.
When you read the table, link each row to a decision: the number of rows changed because of the duplicates, the number of city and department variants changed because of standardizing the spellings, and the age measures changed because impossible values were emptied. Also report the number of rows with an age: the clean mean now rests on fewer rows.
Finally, compare two rows of the table: the mean age changed a lot, and the median age did not change at all. Choose the reason from the options below and write the letter in the reason variable.
This step's code cell is for members.Sign up free
Before you report to the president, make two things clear: the type of each cleaning decision, and what the clean table does and does not say.
1. Decision types. In lesson 21 the decision starts with a question: is it certain that this value is an error? Four decisions:
Match these four situations with decisions and write the letters in the decisions dictionary: "age_210" (a member whose age was written as 210), "age_33" (above the IQR fence, real according to the club records), "st_clair" (a city written as "St. Clair"), "exact_duplicate" (a row written twice during the export).
2. The limit of the report. The clean table has 140 members and the mean age is 20.3. Which sentence does this result allow? Write the letter in the claim variable.
If you had written the mean age (20.3) in place of the 5 ages you emptied, what would have changed in the report?
This step's code cell is for members.Sign up free