Data Analysis for Beginners › Module 1: Getting Started › Lesson 1.2

Your First Look at Data

Free previewLesson 1.2 · about 30 minutes · 5 exercises

0 of 5 exercises completed
Python and the data tools load when you first press Run

Get the full course

10 modules of hands-on data analysis with Python — real tables, real charts, in your browser, no installs. Lessons 1.1 and 1.2 are free; the rest of the course is Rs. 300 + 18% GST.

Enroll Now — Rs. 354.00

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
How the code boxes work

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.

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.

FileWhat it holds
tea_sales.csvEvery item sold by the Malabar Tea House in 2025: 7,202 rows
menu.csvThe tea house's menu, with prices before and after July
tea_sales_raw.csvA messy, hand-typed extract of the sales, for practising cleaning (Module 4)
rainfall.csvMonthly rainfall in millimetres, 2021 to 2025, for Calicut, Cochin, Madras, Bombay and Simla
students.csv, subjects.csv, marks.csvA 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.

0 of 5 exercises completed

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?

2. What does sales.head() give, with nothing in the brackets?

3. sales["Item"] stops with KeyError: 'Item'. What is the most likely cause?

4. Why does every box start with import pandas as pd?

Summary

  • import pandas as pd loads pandas; pd.read_csv("file.csv") reads a CSV file into a table, called a DataFrame.
  • head() and tail() show the first and last rows (five unless you give a number).
  • shape gives (rows, columns); len(table) gives the number of rows; columns gives 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”.