1
Hatchling0 / 300 XP
0day streak 0stars
Your Python practice
Mock tests Readiness
More
Guest mode · saved on this device.

Open a CSV, inspect the table

A CSV is a text file with a header and one record per line. pd.read_csv("file.csv") loads it into a DataFrame, like a spreadsheet table with named columns.

import pandas as pd
bookings = pd.read_csv("bookings.csv")
preview = bookings.head(3)
dimensions = bookings.shape
names = list(bookings.columns)

head(3) returns the first three rows, not a random sample. shape is a (rows, columns) tuple: bookings.shape[0] counts rows and bookings.shape[1] counts columns. columns contains the header labels. These two are attributes: no parentheses.

Before calculating anything, inspect the headers and a few rows. A column called minutes should not silently be treated as hours. Each attached CSV is fictional and downloadable.

See it run

import pandas as pd
rows = "route,parcels\nnorth,12\neast,7\nwest,9\nsouth,15\n"
open("trips.csv", "w").write(rows)
trips = pd.read_csv("trips.csv")
top = trips.head(2)
size = trips.shape
cols = list(trips.columns)

A CSV is text. read_csv turns it into a table you can inspect.

Practice open a csv, inspect the table

Exercise 1

You coordinate a local delivery depot. Load depot.csv into deliveries. Save its first three rows as preview, its shape as dimensions, and its column names as the list names. Print the preview and dimensions so the next coordinator can inspect them.

Exercise 2

A support lead sent helpdesk.csv. Load it as tickets, save the first four records as preview, save the number of rows as ticket_count, and save the header list as names. Print the preview. One row is one ticket, not one minute of work.

Exercise 3

You are checking a facilities stock export. Read supply_shelf.csv into stock, save the first two rows as preview, the shape as dimensions, and the column names as names. Print names and preview before anyone places an order.

The explanation and tasks are public. Running this lesson requires an account with course access.

Practice

Loading Python…
More
Loading exercise content…
Visualizer Watch your code run line by line

Run your code, then choose Play to follow its execution.

Memory · names → values
Variables appear here as Python creates them.
Output & plots

One-line daily quest