Unit 4 • Lesson 6

Reading a CSV into pandas

Overview

One line replaces the whole of unit 1, and then you check what you got.

Intermediate 20–25 min

What You Will Learn in This Lesson

By the end of this lesson, you will be able to:

  • Read a CSV into a DataFrame in one call.
  • Say what read_csv decides for you, and what it cannot know.
  • Run the same four checks on every table you load.
  • Recognise the shape of a load that went wrong.

Why This Matters

Unit 1 built a table out of lists and dicts by hand so that you know what a table is. This lesson replaces all of it with one line — and the point of having done it by hand is that you now know what that line is deciding on your behalf, and which of those decisions to check.

Step 1: read_csv

It reads the header, splits the rows, and guesses each column's type. What it cannot do is know what your file means.

1

The whole of unit 1, in one call

Opening the file, reading the header, splitting each line, converting the numbers — all of it, in the one call.

import pandas as pd

df = pd.read_csv("scores.csv")
print(df.shape)
print(df.dtypes)
2

What it decided for you

Three decisions, every time: the first line is column names, the separator is a comma, and each column's type is inferred from what it saw. Each is usually right and each is occasionally wrong.

import pandas as pd

# Each of these overrides one of those three decisions.
# df = pd.read_csv("f.csv", header=None)
# df = pd.read_csv("f.csv", sep=";")
# df = pd.read_csv("f.csv", dtype={"id": str})
print("the defaults are decisions, not facts")

Keeping an id column as text with dtype={"id": str} is the common one. An id that looks numeric loses its leading zeros the moment pandas reads it as a number.

Mini Practice #1: Read from a string

Try It Yourself

StringIO lets you practise read_csv without a file. Read this text and print its shape and dtypes.

Press Run to see output

Step 2: Check before you trust

head shows you the first rows and dtypes shows you what pandas decided. Two lines, and they catch most of what goes wrong.

1

The four questions again

The same four from lesson two, now on a table you did not build. This is the habit worth keeping for the rest of the course.

import pandas as pd

df = pd.read_csv("scores.csv")
print(df.shape)            # how much
print(df.dtypes)           # what kind
print(df.head(2))          # does it line up
print(df.isna().sum())     # how much is missing
2

What a failed load looks like

A wrong separator does not raise. Every line arrives as one long string, so you get one column and a shape that tells you immediately.

import pandas as pd
from io import StringIO

text = "name;score\nAda;92\nGrace;88\n"
wrong = pd.read_csv(StringIO(text))
print(wrong.shape)
print(list(wrong.columns))

right = pd.read_csv(StringIO(text), sep=";")
print(right.shape)
print(list(right.columns))

Mini Practice #2: Diagnose the load

Try It Yourself

This load produces one column where there should be three. Fix it with one argument.

Press Run to see output

Step 3: When to Use It

Any tabular file

read_csv replaces the reader loop, the header handling and the conversions in one call.

df = pd.read_csv("scores.csv")

A first look at new data

Follow it with shape, dtypes and head() to see what actually arrived.

When Not to Use It

Do not trust the load because it did not raise. A wrong separator gives one column, and a missing marker turns numbers into text — both silently.

End-of-Lesson Exercises

Exercise 1: The average from a file

A file scores.csv is supplied when your work is checked, with name and score columns. Write average_score(path) that reads it and returns the mean score, rounded to two decimal places.

pd.read_csv(path), then the mean of the score column, then round to two places. The second check swaps the file, so a typed-in answer fails.

Press Run to try it, then Check my work to mark it.

Exercise 2: Report on a loaded file

Write load_report(path) that reads the file and returns a tuple of (row count, column count, sorted column names). Read it rather than assuming what is in it.

Read once into a variable, then take shape[0], shape[1] and sorted(df.columns) off it.

Press Run to try it, then Check my work to mark it.