By the end of this lesson you will be able to
- load a CSV file into pandas with
pd.read_csv() - look at a table's first and last rows, its size and its column names
- add up a column and count the rows, to answer your first questions
- recognise and fix the four mistakes beginners make most often
Every box on this page holds real Python that you can change. Press Run to run it; the result appears underneath. Reset puts back the original code. The first Run on any page loads Python and the data tools, which takes a little while; after that, boxes run in a moment.
On a phone: brackets, quotes and the full stop are on the symbols keyboard (the ?123 key). If your keyboard types curly quotes “ ”, the page offers to fix them. A wide table in the output can be scrolled sideways.
On a computer: press Ctrl + Enter (⌘ + Enter on a Mac) to run.
Reading a file
Data usually arrives as a file, and the most common kind is a CSV file (“comma-separated values”): plain text with one row of the table on each line and commas between the columns. The first line holds the column names. The tea house's menu, for example, starts like this:
item,category,price_jan,price_jul,vegetarian Masala Chai,Tea,30,35,yes Nilgiri Green Tea,Tea,40,45,yes
Here is how to load that file into Python and show it. Press Run.
Three lines, and the whole menu appears as a neat table. Let us take them one at a time.
import pandas as pdloads the pandas tool and gives it the short namepd. Every program in this course starts with this line (almost every data analyst in the world writes it the same way).pd.read_csv("menu.csv")reads the file and turns it into a table. The file name goes inside quotes. The practice files are already in place, so the name alone is enough.menu = …stores the table under the namemenu, so that the next lines can use it. A name like this is called a variable; Lesson 1.3 says more about them.print(menu)shows it.
pandas calls a table a DataFrame. Notice the numbers 0 to 11 down the left-hand side: they are not part of the file. pandas gives every row a label, called the index, starting from 0.
Big tables: head, tail and shape
The menu has only twelve rows. The sales file has thousands. Printing it shows only the first and last five rows, with ... in between, and its size at the bottom:
Usually you want a quick look at the start of a table, not the whole thing. That is what head() is for. It gives the first five rows, or as many as you put in the brackets; tail() gives the last rows.
Things written after a dot, like .head(), are called methods: actions that belong to the table. Some things after a dot are simply facts about the table, with no brackets. Two you will use constantly are shape, the number of rows and columns, and columns, the column names:
(7202, 8) means 7,202 rows and 8 columns: rows always come first. len(sales) gives just the number of rows. The column list is worth checking every time you open a new file, because you will need the names exactly as they are written, including capital letters and underscores.
Your first answers
To work with one column, put its name in square brackets and quotes after the table's name: sales["quantity"]. Then you can ask questions of it. How many items did the tea house sell in the whole year?
Lines that start with # are comments: notes for people reading the code, which Python ignores. And print() can show several things at once, separated by commas, with a space between them.
Try changing sum() to max(), then to min(): the largest and smallest quantity on any single row. You will meet many more of these in Module 2.
The practice files
These files are ready for you to use in any box. You will meet them all during the course.
| File | What it holds |
|---|---|
| tea_sales.csv | Every item sold by the Malabar Tea House in 2025: 7,202 rows |
| menu.csv | The tea house's menu, with prices before and after July |
| tea_sales_raw.csv | A messy, hand-typed extract of the sales, for practising cleaning (Module 4) |
| rainfall.csv | Monthly rainfall in millimetres, 2021 to 2025, for Calicut, Cochin, Madras, Bombay and Simla |
| students.csv, subjects.csv, marks.csv | A small school: pupils, subjects and termly marks (Module 7) |
Four common mistakes and how to fix them
Everyone who writes code makes mistakes, and analysts see error messages every day. What matters is being able to read them. Python's message ends with the most useful line: the kind of error and what went wrong. This page also explains each one in plain English. Run each box below, read the message, then fix the code.
1. Forgetting to import pandas
Python says NameError: name 'pd' is not defined: it has never heard of pd, because the import pandas as pd line is missing. Each box starts fresh, so every box needs its own import line at the top.
2. A misspelt file name
A FileNotFoundError means there is no file with that exact name. The file is tea_sales.csv, with an underscore, not a hyphen.
3. A misspelt column name
A KeyError: 'Quantity' means the table has no column with that name. Column names must match exactly, and here the real name is quantity, with a small q. When in doubt, print(sales.columns).
4. Missing quotes
Without quotes, Python thinks menu.csv is the name of something in the program, not a file name, and stops with a NameError. File names and column names always go inside quotes.
Exercises
Now it is your turn. Each exercise shows the expected output (it appears once Python has loaded). Type your code in the box and press Check my answer. The checker runs your code and compares it with the model solution. Some exercises check what your code prints; others check a result you store under a given name, so use exactly the name the task asks for. If you get stuck, Show solution becomes available after your first check.
Exercise 1 · Show the menu
Load menu.csv into a table called menu, then print the whole table.
Exercise 2 · The first three sales
Load tea_sales.csv into a table called sales, then print only its first three rows.
Exercise 3 · How big is the rainfall table?
Load rainfall.csv into a table called rain. Store its number of rows in a variable called rows, and print rows.
Exercise 4 · Total rainfall
Load rainfall.csv, then store the total of its rainfall_mm column (all towns and all months added together) in a variable called total_rain. Print it.
Exercise 5 · Find and fix the mistakes
This program should print how many items the tea house sold in 2025, stored in a variable called total_items. It contains three mistakes. Run it after each fix: each error message points you to the next problem.
Quick check
Four questions to confirm the main ideas. Choose an answer to see the explanation.
1. print(sales.shape) shows (7202, 8). What does that mean?
shape always gives rows first, then columns. The sales file has 7,202 rows (one per item sold) and 8 columns.
2. What does sales.head() give, with nothing in the brackets?
Without a number, head() gives five rows. head(3) gives three, and tail() gives the last rows. The column names come from sales.columns.
3. sales["Item"] stops with KeyError: 'Item'. What is the most likely cause?
A KeyError on a column means no column has exactly that name. Names must match exactly, including capital letters. A missing import would give a NameError, not a KeyError.
4. Why does every box start with import pandas as pd?
import makes the pandas tool available under the short name pd. Every box runs on its own, so a box without the import line cannot use pd. Reading the file is pd.read_csv()'s job.
Summary
import pandas as pdloads pandas;pd.read_csv("file.csv")reads a CSV file into a table, called a DataFrame.head()andtail()show the first and last rows (five unless you give a number).shapegives (rows, columns);len(table)gives the number of rows;columnsgives the column names.table["column"]picks out one column;.sum(),.max()and.min()summarise it.- Error messages name the problem on their last line: NameError (a missing import or quotes), FileNotFoundError (a wrong file name), KeyError (a wrong column name).
Found a mistake on this page, or something unclear? Report a problem and mention “Data Lesson 1.2”.
