Data usually arrives row by row, but a DataFrame keeps it column by column. Turn one into the other.
What to build
A program that turns comma-separated rows into a column store and prints it, column by column.
Requirements
- Line 1 is the header with the column names; each later line is one row with one value per column.
- Store the table as a dictionary of lists, one per column, then print
NAME: v1, v2, ...for each column in header order, and finallyshape: (ROWS, COLUMNS).
Input
Line 1: the header. Line 2: n. Then n rows; values are separated by commas and contain no commas.
Output
One line per column, then the shape.
Constraints
1 ≤ columns ≤ 10; 0 ≤ n ≤ 100.
Example
Input:
name,age,city
3
Ada,36,London
Rahim,24,Dhaka
Mei,29,Tokyo
Output:
name: Ada, Rahim, Mei
age: 36, 24, 29
city: London, Dhaka, Tokyo
shape: (3, 3)
Why it works
A DataFrame is a set of equal-length columns, so building the column lists from the rows is exactly the reshaping that pandas does when it reads a file.
Examples
name,age,city 3 Ada,36,London Rahim,24,Dhaka Mei,29,Tokyo
name: Ada, Rahim, Mei age: 36, 24, 29 city: London, Dhaka, Tokyo shape: (3, 3)
x 2 1 2
x: 1, 2 shape: (2, 1)
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Make a dictionary with one empty list per header name, then append each row's values to the right lists.
Hint 2
Join each column's values with ", " when printing.
Solution
Show one correct solution (try it yourself first)
header = input().split(",")
n = int(input())
table = {name: [] for name in header}
for _ in range(n):
values = input().split(",")
for name, value in zip(header, values):
table[name].append(value)
for name, values in table.items():
print(f"{name}: {', '.join(values)}")
print(f"shape: ({n}, {len(header)})")