# Loading a CSV File

pd.read_csv loads a CSV file into a DataFrame, usecols and nrows load only the columns and rows you need, sep reads a file whose values are split by semicolons, and a FileNotFoundError means the path doesn't lead from the folder the notebook runs in to the file.

Apprentice · Python · Updated November 19, 2026

By Gregor Dzierzon

## After this lesson you can

- Load a CSV file into a DataFrame with pd.read_csv, using usecols, nrows and sep when the file needs them
- Fix a FileNotFoundError by finding the folder a notebook runs in and writing the path to the file from there

## Key takeaways

- pd.read_csv("sales.csv") reads a CSV file into a DataFrame, the first line becoming the column names.
- usecols= loads only the columns you list, and nrows= only the first rows, which is a quick way to peek at a very large file.
- sep= names the character between values when it isn't a comma. A file with semicolons read without sep=";" comes in as one long column.
- A path such as "sales.csv" is looked for from the folder the notebook runs in. A FileNotFoundError means it isn't there, and Path.cwd() shows which folder that is.

Most data reaches you as a file. A teacher posts a dataset for a class project, a manager exports last month's sales from a spreadsheet, an app lets you download your history: very often the file is a CSV. Loading it into a pandas table is the first line of almost every analysis, and this lesson is that line, with the options and the error you'll meet most.

The example is Sunnyside Farmers Market's sales for the 2026 season, `sales.csv`: one row for each product a vendor sold on a Saturday, 1,900 rows from April to October. The market and its vendors are our own made-up data.

## From a file to a DataFrame

A **CSV file** ("comma-separated values") is a table saved as plain text: one row per line, with commas between the values, and the column names on the first line. These are the first lines of `sales.csv`:

```csv
date,vendor_id,product,category,units,price
2026-04-04,V01,lettuce,produce,21,3.00
2026-04-04,V01,kale,produce,18,3.50
2026-04-04,V01,spinach,produce,21,4.00
```

pandas is the Python library for tables, imported as `pd`. **`pd.read_csv`** reads the whole file into a DataFrame, pandas' table:

```python
import pandas as pd

sales = pd.read_csv("sales.csv")
sales
```

```text
            date vendor_id         product category  units  price
0     2026-04-04       V01         lettuce  produce   21.0    3.0
1     2026-04-04       V01            kale  produce   18.0    3.5
2     2026-04-04       V01         spinach  produce   21.0    4.0
3     2026-04-04       V02       honey jar    honey   20.0   12.0
4     2026-04-04       V02       honeycomb    honey    4.0   15.0
...          ...       ...             ...      ...    ...    ...
1895  2026-10-31       V15     dried herbs  produce   13.0    6.0
1896  2026-10-31       V16        knit hat   crafts    3.0   25.0
1897  2026-10-31       V16           scarf   crafts    2.0   30.0
1898  2026-10-31       V16        tote bag   crafts    6.0   15.0
1899  2026-10-31       V16  dish cloth set   crafts    9.0   10.0

[1900 rows x 6 columns]
```

A big table shows its first five and last five rows, with `...` for the rest, and its size at the bottom: 1,900 rows and 6 columns. VS Code draws the table as a grid; this lesson shows it as pandas prints it as text. pandas worked out each column's type from the values, so `units` and `price` came in as numbers. (`units` shows `21.0` because a few units are blank in the file, which *A First Look: head, info and describe* explains.)

## Only what you need: usecols and nrows

A file can have far more columns or rows than one question needs. **`usecols`** loads only the columns you list, and **`nrows`** only the first rows, not counting the header line:

```python
pd.read_csv("sales.csv", usecols=["date", "product", "units"], nrows=5)
```

```text
         date    product  units
0  2026-04-04    lettuce     21
1  2026-04-04       kale     18
2  2026-04-04    spinach     21
3  2026-04-04  honey jar     20
4  2026-04-04  honeycomb      4
```

`nrows` is handy for a peek at a file too big to open comfortably. The names in `usecols` must match the file exactly: `usecols=["Product"]` stops with `ValueError: Usecols do not match columns, columns expected but not found: ['Product']`.

## Another separator: sep

Hollow Oak Woodworks, one of the vendors, sends its own record from a spreadsheet that puts a semicolon between values, as many spreadsheets do where the comma is the decimal point:

```csv
date;product;units;price
2026-04-04;cutting board;5;35.00
2026-04-04;wooden spoon;9;9.00
2026-04-11;cutting board;3;35.00
2026-04-11;wooden spoon;7;9.00
```

`read_csv` looks for commas, finds none, and makes each whole line one value in a single column, `date;product;units;price`. **`sep`** names the separator:

```python
pd.read_csv("hollow_oak.csv", sep=";")
```

```text
         date        product  units  price
0  2026-04-04  cutting board      5   35.0
1  2026-04-04   wooden spoon      9    9.0
2  2026-04-11  cutting board      3   35.0
3  2026-04-11   wooden spoon      7    9.0
```

Four columns, as it should be. A table with one column whose name is full of semicolons or tabs is the sign a file needs `sep` (`sep="\t"` for tabs).

## When the file isn't found

Type the name slightly wrong and pandas stops:

```python
pd.read_csv("sale.csv")
```

```text
FileNotFoundError: [Errno 2] No such file or directory: 'sale.csv'
```

That's the last line of the error. `"sale.csv"` is a **relative path**: pandas looks for it starting from the folder the notebook runs in, its working folder. In VS Code, a notebook runs in the folder it's saved in. When you're sure the name is right, check that folder:

```python
from pathlib import Path

print(Path.cwd())
```

```text
C:\Users\Sam\python-course\market
```

`Path.cwd()` ("current working directory") shows the folder pandas looks in; on a Mac it starts `/Users/`. If the CSV file sits in a folder inside that one, say `data`, the path names the folder too, with a forward slash, which works on Windows and on a Mac:

```python
pd.read_csv("data/sales.csv").shape
```

```text
(1900, 6)
```

## Check yourself

2 questions to try on the lesson page.

## So what

`pd.read_csv` with the right path and options turns a file into a table in one line, and when it can't find the file, `Path.cwd()` tells you where it looked.

## Key terms

- [CSV File](https://www.ask-codey.com/glossary/csv-file)
- [read_csv()](https://www.ask-codey.com/glossary/read-csv)
- [Relative Path](https://www.ask-codey.com/glossary/relative-path)
- [FileNotFoundError](https://www.ask-codey.com/glossary/filenotfounderror)

## Related

- [DataFrames: Rows, Columns and the Index](https://www.ask-codey.com/lessons/pandas-dataframes)
- [A First Look: head, info and describe](https://www.ask-codey.com/lessons/pandas-head-info-describe)
- [Loading an Excel Workbook](https://www.ask-codey.com/lessons/pandas-read-excel)

Source: https://www.ask-codey.com/lessons/pandas-read-csv
