Data science: A missing-values report

The first question to ask of any table is how much of it is missing. Report the missing values of every column and how many rows are complete.

What to build

A program that profiles the missing values of a small CSV table.

Requirements

  • For each column, in order, print NAME: M missing (P%), where P is the percentage of rows, rounded to a whole number.
  • Then print complete rows: K of N.
  • A cell is missing when it is empty or is exactly NA (after removing spaces around it).

Input

Line 1: n, the number of data rows. Line 2: the header. Then n rows, cells separated by commas.

Output

One line per column, then one more line.

Constraints

1 ≤ n ≤ 1000; every row has as many cells as the header; no cell contains a comma.

Example

Input:

4
name,age,city
Ana,31,Paris
Ben,,Rome
Cy,NA,
Dee,40,Oslo

Output:

name: 0 missing (0%)
age: 2 missing (50%)
city: 1 missing (25%)
complete rows: 2 of 4

Why it works

Counting missing cells per column shows which variables are unreliable, and counting complete rows shows how much data would survive if incomplete rows were dropped.

Examples

Example 1 · Input
4
name,age,city
Ana,31,Paris
Ben,,Rome
Cy,NA,
Dee,40,Oslo
Expected output
name: 0 missing (0%)
age: 2 missing (50%)
city: 1 missing (25%)
complete rows: 2 of 4
Example 2 · Input
3
a,b
1,2
3,4
5,6
Expected output
a: 0 missing (0%)
b: 0 missing (0%)
complete rows: 3 of 3

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

Hints

Hint 1

Split each row with split(","); an empty cell and the text NA both count as missing.

Hint 2

A row is complete when none of its cells is missing.

Hint 3

round(100 * missing / n) gives the whole-number percentage.

Solution

Show one correct solution (try it yourself first)
n = int(input())
header = input().split(",")
missing = [0] * len(header)
complete = 0
for _ in range(n):
    cells = [c.strip() for c in input().split(",")]
    gaps = [c == "" or c == "NA" for c in cells]
    for i, gap in enumerate(gaps):
        if gap:
            missing[i] += 1
    if not any(gaps):
        complete += 1
for name, m in zip(header, missing):
    print(f"{name}: {m} missing ({round(100 * m / n)}%)")
print(f"complete rows: {complete} of {n}")

Lesson: Exploratory data analysis →