pandas: A pipeline of named steps

A cleaning chain is often written as df.pipe(dropna).pipe(strip).pipe(dedupe)... Run the steps in the order given and log the row count after each.

What to build

A program that runs named cleaning steps on name and age rows.

Requirements

  • Steps: dropna removes rows with an empty name or age; strip trims the name, collapses inner spaces and converts it to title case; dedupe removes rows identical to an earlier row (comparing the current values); adults keeps ages of 18 or more; sort sorts by age, then by name.
  • After each step print STEP: N rows. Then print name,age and the remaining rows.

Input

Line 1: n. Then n lines NAME,AGE. Last line: the steps separated by spaces.

Output

One line per step, then 1 + K lines.

Constraints

1 ≤ n ≤ 1000; ages are whole numbers or empty; adults and sort only run when no age is empty.

Example

Input:

6
 ana ,30
Ben,17
ANA,30
,22
cy,45
Dee,
dropna strip dedupe adults sort

Output:

dropna: 4 rows
strip: 4 rows
dedupe: 3 rows
adults: 2 rows
sort: 2 rows
name,age
Ana,30
Cy,45

Why it works

Each step receives the output of the previous one, so the order matters: removing duplicates before cleaning names misses rows that only differ in spacing or case, and the row counts show exactly what each step did.

Examples

Example 1 · Input
6
 ana ,30
Ben,17
ANA,30
,22
cy,45
Dee,
dropna strip dedupe adults sort
Expected output
dropna: 4 rows
strip: 4 rows
dedupe: 3 rows
adults: 2 rows
sort: 2 rows
name,age
Ana,30
Cy,45
Example 2 · Input
2
b,20
a,20
sort
Expected output
sort: 2 rows
name,age
a,20
b,20

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

Keep the rows as a list of [name, age] pairs and apply one function per step.

Hint 2

After every step, print the step's name and the number of rows.

Hint 3

Sort with key=lambda r: (int(r[1]), r[0]).

Solution

Show one correct solution (try it yourself first)
def dropna(rows):
    return [r for r in rows if r[0].strip() and r[1].strip()]


def strip(rows):
    return [[" ".join(r[0].split()).title(), r[1]] for r in rows]


def dedupe(rows):
    seen, out = set(), []
    for r in rows:
        if tuple(r) not in seen:
            seen.add(tuple(r))
            out.append(r)
    return out


def adults(rows):
    return [r for r in rows if int(r[1]) >= 18]


def sort(rows):
    return sorted(rows, key=lambda r: (int(r[1]), r[0]))


STEPS = {"dropna": dropna, "strip": strip, "dedupe": dedupe, "adults": adults, "sort": sort}
n = int(input())
rows = [input().split(",") for _ in range(n)]
for step in input().split():
    rows = STEPS[step](rows)
    print(f"{step}: {len(rows)} rows")
print("name,age")
for name, age in rows:
    print(f"{name},{age}")

Lesson: Method chaining, apply and performance →