Skip to main content

How to Read a CSV File into a pandas DataFrame with read_csv

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

A CSV file is a table saved as plain text, one row per line with commas between the values. pandas' read_csv loads it into a DataFrame, using the first line for the column names and choosing a type for each column. This guide reads a two-person file, with real output from pandas 3.1.1.

Syntax​

Syntax
<dataframe> = pd.read_csv("<file>.csv")
PartWhat it means
"<file>.csv"The path of the file, relative to the folder you run Python in
The resultA DataFrame. The file's first line becomes the column names

The pandas reference: "Read a comma-separated values (csv) file into DataFrame."

Prerequisites​

  • pandas in your project, for example with uv add pandas. This guide uses pandas 3.1.1 on Python 3.15.2.

Step 1: Create the File and the Script​

Save this as people.csv:

people.csv
name,age
Ada,36
Grace,45

Then the script, in the same folder:

read_people.py
import pandas as pd

people = pd.read_csv("people.csv")

print(people)
print(people.dtypes)

The read_people.py script in a macOS-style code window: import pandas, people = pd.read_csv("people.csv"), then print(people) and print(people.dtypes).

Step 2: Run It​

Terminal
uv run python read_people.py
Output
name age
0 Ada 36
1 Grace 45
name str
age int64
dtype: object

The script's output in a terminal: the DataFrame with name and age for Ada and Grace, then the types, str for name and int64 for age.

The first line became the columns name and age. pandas read the ages as whole numbers, int64, and the names as text, which pandas 3 shows as str.

Common Mistakes​

MistakeWhat happensDo this instead
A typo in the file name, or the wrong folderFileNotFoundError: [Errno 2] No such file or directory: 'peple.csv'Check the name, and run Python in the folder that holds the file
A file without a header lineThe first row of data becomes the column names: for Ada,36 and Grace,45, the columns were Ada and 36, with one row leftPass header=None and names=["name", "age"]: both rows were then read as data

Troubleshooting​

SymptomLikely causeFix
FileNotFoundError though the file existsPython runs in another folder than the file'sUse the full path, or cd to the file's folder first

FAQ​

How do I see the column types?​

people.dtypes, as in the script: one line per column with its type.

Can it read other separators?​

Yes, with sep: for example pd.read_csv("file.csv", sep=";") for semicolons.

Conclusion​

pd.read_csv("people.csv") loaded the file into a DataFrame with the columns name and age, typed str and int64.

Additional Resources​