A profile is a one-line summary of every column, the first output of almost every analysis. Decide each column's kind and summarise it.
What to build
A program that profiles each column of a CSV table.
Requirements
- Empty cells are missing and are ignored in the summaries. If every cell of a column is empty, print
NAME: empty. - If every non-empty cell is a number, print
NAME: numeric, missing M, min A, max B, mean C, with A and B in:gformat and C to two decimals. - Otherwise print
NAME: text, missing M, U unique, top T (K): the most common value and its count, taking the alphabetically first value on a tie.
Input
Line 1: n. Line 2: the header. Then n rows, cells separated by commas.
Output
One line per column, in order.
Constraints
1 ≤ n ≤ 1000; no cell contains a comma; numbers are between −100000 and 100000.
Example
Input:
5
city,temp,sky
Oslo,12.6,cloudy
Rome,24,sunny
Oslo,,rain
Paris,18,cloudy
Rome,21,sunny
Output:
city: text, missing 0, 3 unique, top Oslo (2)
temp: numeric, missing 1, min 12.6, max 24, mean 18.90
sky: text, missing 0, 3 unique, top cloudy (2)
Why it works
Trying float() on every value is a reliable test of a numeric column, and the summaries that fit a column depend on its kind: range and mean for numbers, distinct values and the most common one for text.
Examples
5 city,temp,sky Oslo,12.6,cloudy Rome,24,sunny Oslo,,rain Paris,18,cloudy Rome,21,sunny
city: text, missing 0, 3 unique, top Oslo (2) temp: numeric, missing 1, min 12.6, max 24, mean 18.90 sky: text, missing 0, 3 unique, top cloudy (2)
3 id,score 1,90 2,85 3,95
id: numeric, missing 0, min 1, max 3, mean 2.00 score: numeric, missing 0, min 85, max 95, mean 90.00
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
A column is numeric when every non-empty cell can be converted with float(); try it inside try/except ValueError.
Hint 2
For text columns, count values with a dictionary and pick the top one with min(counts, key=lambda v: (-counts[v], v)).
Hint 3
f"{x:g}" prints 24.0 as 24 and 12.6 as 12.6.
Solution
Show one correct solution (try it yourself first)
n = int(input())
header = input().split(",")
rows = [input().split(",") for _ in range(n)]
for i, name in enumerate(header):
values = [r[i] for r in rows if r[i] != ""]
missing = n - len(values)
if not values:
print(f"{name}: empty")
continue
try:
nums = [float(v) for v in values]
except ValueError:
nums = None
if nums is not None:
mean = sum(nums) / len(nums)
print(f"{name}: numeric, missing {missing}, min {min(nums):g}, max {max(nums):g}, mean {mean:.2f}")
else:
counts = {}
for v in values:
counts[v] = counts.get(v, 0) + 1
top = min(counts, key=lambda v: (-counts[v], v))
print(f"{name}: text, missing {missing}, {len(counts)} unique, top {top} ({counts[top]})")