Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Loading Data  ›  Lesson

Reading CSV Files

CSV 14 minread_csv loads comma-separated text into a DataFrame, but separators, header rows, missing markers and dates often need to be stated, and the result must be checked.

Reading CSV Files

Until now sales was typed by hand. In real life the bookshop's till exports a file called sales.csv every night, and your analysis starts by loading it. CSV is the most common data file in the world, and also the one most likely to be slightly wrong.

The rule

read_csv guesses; tell it what it cannot know, then check what it built.

A CSV file is plain text: one line per row, values separated by commas, usually with a header line of column names.

date,branch,genre,copies,price
2024-03-01,Pune,fiction,3,250
2024-03-01,Delhi,science,5,400
sales = pd.read_csv("sales.csv")

pandas assumes values are split by commas and that the first line is the header, and it guesses each column's type from the values. When the file is unusual, you supply the facts it cannot guess:

pd.read_csv(
    "sales_export.csv",
    sep=";",                     # values split by semicolons, common in Europe
    skiprows=2,                  # two lines of title text before the header
    usecols=["date", "branch", "copies"],   # load only these columns
    parse_dates=["date"],        # turn the date text into real dates
    na_values=["-", "unknown"],      # treat these markers as missing
    encoding="utf-8",
)

Then check what it built, every time:

sales.shape       # did every row arrive?
sales.head()      # do the columns look right?
sales.dtypes      # are numbers numbers?
sales.info()      # types plus non-missing counts per column

Writing goes the other way: sales.to_csv("clean_sales.csv", index=False). Without index=False, the 0, 1, 2… index is saved as an extra unnamed column.

The shortcut

After every load, run the three-glance check: shape, head(), dtypes. Most CSV problems show up in one of them within ten seconds.

A file that loads "fine" but is notStep 1 of 4
  1. Load it

    pd.read_csv("sales_export.csv") runs with no error.

Another classic: a copies column showing dtype object instead of int64. Some cell holds text such as "unknown". Adding it to na_values turns it into a proper missing value, and the column becomes numeric (as floats, because NaN is a float).

Drills

Answer fast — trust the rule.

quick drill

A CSV loads with shape (500, 1) and every cell contains semicolons. What is the fix?

quick drill

Why pass index=False to to_csv?

quick drill

price loads as dtype object. What is the most likely cause?

quick drill

A column has values 3, 5 and "-". After loading with na_values=["-"], what is its dtype?

Remember

  • read_csv guesses; tell it what it cannot know, then check what it built.
  • Three-glance check after every load: shape, head(), dtypes.
  • Write with index=False unless the index carries real information.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainIt loaded, but is it right?

A colleague loads a CSV and immediately computes totals. Explain the checks you would run first, and two problems those checks commonly reveal along with their fixes.