Model the first three things everyone looks at in a new DataFrame.
What to build
A program that reports a table's shape, its columns and its first rows.
Requirements
- Print
shape: (ROWS, COLUMNS), thencolumns: NAME, NAME, ..., thenhead(K):followed by the first K rows (fewer if the table is shorter), each asINDEX: v1 | v2 | ...with the row's position as INDEX.
Input
Line 1: K. Line 2: the header, names separated by commas. Line 3: n. Then n rows, values separated by commas.
Output
The lines described.
Constraints
1 ≤ K ≤ 20; 0 ≤ n ≤ 100; 1 ≤ columns ≤ 10.
Example
Input:
2
name,age,city
3
Ada,36,London
Rahim,24,Dhaka
Mei,29,Tokyo
Output:
shape: (3, 3)
columns: name, age, city
head(2):
0: Ada | 36 | London
1: Rahim | 24 | Dhaka
Why it works
shape counts rows and columns, columns lists the header, and head takes a slice from the top, so all three are cheap looks at a table before any real work.
Examples
Example 1 · Input
2 name,age,city 3 Ada,36,London Rahim,24,Dhaka Mei,29,Tokyo
Expected output
shape: (3, 3) columns: name, age, city head(2): 0: Ada | 36 | London 1: Rahim | 24 | Dhaka
Example 2 · Input
5 x,y 2 1,2 3,4
Expected output
shape: (2, 2) columns: x, y head(5): 0: 1 | 2 1: 3 | 4
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Store the rows, then print len(rows) and the number of header names.
Hint 2
head(k) shows at most k rows: rows[:k].
Solution
Show one correct solution (try it yourself first)
k = int(input())
header = input().split(",")
n = int(input())
rows = [input().split(",") for _ in range(n)]
print(f"shape: ({n}, {len(header)})")
print("columns:", ", ".join(header))
print(f"head({k}):")
for i, row in enumerate(rows[:k]):
print(f"{i}: {' | '.join(row)}")