DataFrame.info() is the quickest overview of a table's structure. Model its report.
What to build
A program that prints an info-style report for a comma-separated table.
Requirements
- Empty cells are missing. Print
RangeIndex: N entries, 0 to N-1(orRangeIndex: 0 entriesfor an empty table), thenData columns (total K columns):, then for each columnI NAME C non-null DTYPEwith I its position. - DTYPE uses these rules on the non-missing cells:
boolif all are True/False (object if any are missing),int64if all are whole numbers (float64 if any are missing),float64if all are numbers,objectotherwise; a column with no values isfloat64. - Finally print
dtypes: TYPE(COUNT), ...for the dtypes used, in alphabetical order.
Input
Line 1: the header. Line 2: n. Then n rows, values separated by commas.
Output
The report.
Constraints
0 ≤ n ≤ 100; 1 ≤ columns ≤ 10.
Example
Input:
name,age,paid
3
Ada,36,True
Rahim,,False
,29,True
Output:
RangeIndex: 3 entries, 0 to 2
Data columns (total 3 columns):
0 name 2 non-null object
1 age 2 non-null float64
2 paid 3 non-null bool
dtypes: bool(1), float64(1), object(1)
Why it works
info combines the shape, the missing-value counts and the dtypes, the three facts that decide what cleaning a table needs, which is why it is usually the first thing printed in an analysis.
Examples
Example 1 · Input
name,age,paid 3 Ada,36,True Rahim,,False ,29,True
Expected output
RangeIndex: 3 entries, 0 to 2 Data columns (total 3 columns): 0 name 2 non-null object 1 age 2 non-null float64 2 paid 3 non-null bool dtypes: bool(1), float64(1), object(1)
Example 2 · Input
x,y 2 1,2.5 3,4
Expected output
RangeIndex: 2 entries, 0 to 1 Data columns (total 2 columns): 0 x 2 non-null int64 1 y 2 non-null float64 dtypes: float64(1), int64(1)
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Count the non-empty cells of each column; empty cells are missing.
Hint 2
Reuse the dtype rules: bool, int64 (float64 with missing values), float64, otherwise object.
Solution
Show one correct solution (try it yourself first)
def is_number(text, kind):
try:
kind(text)
return True
except ValueError:
return False
def dtype_of(cells):
present = [c for c in cells if c != ""]
missing = len(cells) - len(present)
if not present:
return "float64"
if all(c in ("True", "False") for c in present):
return "object" if missing else "bool"
if all(is_number(c, int) for c in present):
return "float64" if missing else "int64"
if all(is_number(c, float) for c in present):
return "float64"
return "object"
header = input().split(",")
n = int(input())
rows = [input().split(",") for _ in range(n)]
print(f"RangeIndex: {n} entries, 0 to {n - 1}" if n else "RangeIndex: 0 entries")
print(f"Data columns (total {len(header)} columns):")
used = {}
for i, name in enumerate(header):
cells = [row[i] for row in rows]
dtype = dtype_of(cells)
used[dtype] = used.get(dtype, 0) + 1
print(f"{i} {name} {sum(1 for c in cells if c != '')} non-null {dtype}")
print("dtypes: " + ", ".join(f"{t}({used[t]})" for t in sorted(used)))