1
Hatchling0 / 300 XP
0day streak 0stars
Your Python practice
Mock tests Readiness
More
Guest mode · saved on this device.

Clean an export before trusting it

Real exports contain spaces, blank cells, text in numeric fields, and repeated records. Agree on a policy before changing them:

clean["team"] = clean["team"].str.strip()
clean["amount"] = pd.to_numeric(clean["amount"], errors="coerce")
clean = clean.dropna(subset=["amount"])
clean = clean.drop_duplicates(subset=["record"], keep="first")

str.strip() removes surrounding whitespace, not spaces inside a name. to_numeric(..., errors="coerce") turns invalid numeric text into missing values. dropna(subset=[...]) removes rows only when those required fields are missing. fillna(0) is appropriate only when the task says missing really means zero.

Deduplicate on the business key, not on the amount: two legitimate invoices can have the same value. These exercises keep the first occurrence of a repeated key. After filtering, row indexes may have gaps; reset_index(drop=True) gives a fresh row index.

To turn a cleaned numeric column into whole numbers, assign clean["seats"] = clean["seats"].astype(int). A column's dtype is the type used for its values. Drop missing seats first: ordinary integer dtype cannot hold missing values. Before editing a filtered table, clean = clean.copy() makes an independent table to update.

See it run

import pandas as pd
raw = pd.DataFrame({"claim": [" a1", "b2 ", "c3", "d4"], "amount": ["20", "x", "35", "10"]})
raw["claim"] = raw["claim"].str.strip()
raw["amount"] = pd.to_numeric(raw["amount"], errors="coerce")
tidy = raw.dropna(subset=["amount"])

Cleaning is a chain of small, visible fixes. Watch the table after each one.

Practice clean an export before trusting it

Exercise 1

Clean an expense clerk's expense_export.csv into clean. Strip claim and team, convert amount to numbers (invalid text becomes missing), drop rows with missing amount, then keep the first occurrence of each claim. Keep columns claim, team, amount in source order. Do not invent a zero expense for an unknown amount.

Exercise 2

A delivery manifest uses blank returned to mean zero returns. Load returns_manifest.csv as clean. Strip shipment and depot, parse shipped and returned as numbers, fill missing returns with zero, drop rows missing shipped, and keep the first row per shipment. Preserve the four source columns and their order.

Exercise 3

Prepare a training attendance export as clean from workshop_attendance.csv. Strip booking and course, convert seats to numeric, remove missing/invalid seats, keep the first record per booking, and convert the remaining seats to integer dtype. Two different bookings for the same number of seats must both survive. Keep source column order.

The explanation and tasks are public. Running this lesson requires an account with course access.

Practice

Loading Python…
More
Loading exercise content…
Visualizer Watch your code run line by line

Run your code, then choose Play to follow its execution.

Memory · names → values
Variables appear here as Python creates them.
Output & plots

One-line daily quest