pandas: Describe the numeric columns

Model DataFrame.describe() for numeric columns: the summary you run on every new table.

What to build

A program that prints count, mean, std, min, median and max for each numeric column.

Requirements

  • Skip the columns that are not numeric (any value that is not a number). For each numeric column, in header order, print NAME: count=C mean=M std=S min=A 50%=D max=B, where the numbers other than count have two decimals.
  • std is the sample standard deviation (dividing by count - 1), or NaN when the count is 1. The 50% value is the median: the middle value, or the mean of the two middle values.

Input

Line 1: the header. Line 2: n. Then n rows, values separated by commas.

Output

One line per numeric column.

Constraints

1 ≤ n ≤ 100; 1 ≤ columns ≤ 10.

Example

Input:

name,age,score
4
Ada,36,90
Rahim,24,70
Mei,29,80
Omar,41,60

Output:

age: count=4 mean=32.50 std=7.51 min=24.00 50%=32.50 max=41.00
score: count=4 mean=75.00 std=12.91 min=60.00 50%=75.00 max=90.00

Why it works

describe summarises only the columns it can do arithmetic on, and uses the sample standard deviation, the one statistics uses when the rows are a sample of a larger population.

Examples

Example 1 · Input
name,age,score
4
Ada,36,90
Rahim,24,70
Mei,29,80
Omar,41,60
Expected output
age: count=4 mean=32.50 std=7.51 min=24.00 50%=32.50 max=41.00
score: count=4 mean=75.00 std=12.91 min=60.00 50%=75.00 max=90.00
Example 2 · Input
x,label
1
5,a
Expected output
x: count=1 mean=5.00 std=NaN min=5.00 50%=5.00 max=5.00

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

Hints

Hint 1

A column is numeric when every value converts with float().

Hint 2

The sample standard deviation divides by n - 1; with one value it is NaN.

Solution

Show one correct solution (try it yourself first)
def numeric(cells):
    try:
        return [float(c) for c in cells]
    except ValueError:
        return None


header = input().split(",")
n = int(input())
rows = [input().split(",") for _ in range(n)]
for i, name in enumerate(header):
    values = numeric([row[i] for row in rows])
    if values is None:
        continue
    count = len(values)
    mean = sum(values) / count
    std = "NaN" if count == 1 else f"{(sum((v - mean) ** 2 for v in values) / (count - 1)) ** 0.5:.2f}"
    s = sorted(values)
    median = s[count // 2] if count % 2 else (s[count // 2 - 1] + s[count // 2]) / 2
    print(f"{name}: count={count} mean={mean:.2f} std={std} min={s[0]:.2f} 50%={median:.2f} max={s[-1]:.2f}")

Lesson: DataFrames: tables of labelled columns →