Python Reading CSV Files with Pandas
Table of Contents + โ
In the last lesson, Pandas DataFrames, you learned to inspect and filter a table. But every table so far was typed by hand. That is fine for learning. In real life, nobody types the data. It already exists in a file, usually a CSV. So the very first step of almost every data project is loading that file into a DataFrame. Pandas makes this a one-line job, and this lesson shows you how.
๐ค Why CSV files?
Data has to live somewhere when the program is not running. The most common home for table data is a CSV file. You have probably seen one already without knowing the name.
A few reasons CSV is everywhere:
- It is just plain text, so anything can read it: Excel, Google Sheets, databases, and Python.
- It is small and simple, with no fancy formatting to get in the way.
- Almost every tool can export to it, so you will receive data as CSV all the time.
CSV stands for โcomma-separated valuesโ. It is a text file where each line is a row, and the values in that row are separated by commas. The first line is usually the header, the column names.
๐ What a CSV file looks like
Before we load one, letโs see what is inside. Imagine a file called people.csv with this text:
name,age,city,salaryAlex,25,London,50000Riya,30,Mumbai,60000Arjun,22,Toronto,45000Maria,35,London,80000Sam,28,Mumbai,52000Read it like a table. The first line, name,age,city,salary, is the header with the four column names. Every line after that is one person, with their values in the same order, separated by commas. That is the whole format. Nothing hidden.
๐ก Reading a CSV into a DataFrame
Loading this file is a single call: pd.read_csv(). You give it the file name and it hands you back a ready DataFrame.
This reads the file and shows the first rows:
import pandas as pd
df = pd.read_csv("people.csv")
print(df.head())Output
name age city salary0 Alex 25 London 500001 Riya 30 Mumbai 600002 Arjun 22 Toronto 450003 Maria 35 London 800004 Sam 28 Mumbai 52000That one line did a lot for you:
- It opened the file and read every row.
- It used the first line as the column names automatically.
- It guessed the type of each column, so โageโ and โsalaryโ came in as numbers, not text.
- It added the index (0, 1, 2, โฆ) on the left.
Once it is a DataFrame, everything from the last lesson works. You can filter it, pick columns, and add columns, exactly the same way.
print(df[df["salary"] > 55000])Output
name age city salary1 Riya 30 Mumbai 600003 Maria 35 London 80000Caution
If Python cannot find the file, you get FileNotFoundError. Make sure people.csv sits in the same folder you run the script from, or pass the full path like pd.read_csv("C:/data/people.csv").
โ๏ธ Useful read_csv options
Most CSV files load cleanly with no extra work. But files come in different shapes, so read_csv has options for the common cases. Here are the ones you will reach for.
This shows three handy options:
import pandas as pd
# Use a column from the file as the indexdf = pd.read_csv("people.csv", index_col="name")
# Read only some of the columnsdf2 = pd.read_csv("people.csv", usecols=["name", "salary"])
# Load just the first 3 rows, handy for a peek at a huge filedf3 = pd.read_csv("people.csv", nrows=3)
print(df.head(2))Output
age city salarynameAlex 25 London 50000Riya 30 Mumbai 60000What each option does:
index_col="name"tells Pandas to use the โnameโ column as the row labels, instead of the plain 0, 1, 2. Now you can look people up by name.usecols=["name", "salary"]loads only those two columns and ignores the rest. Great when a file has many columns you do not need.nrows=3reads only the first 3 rows. When a file is huge, this lets you peek at the shape without loading the whole thing.
Tip
Some CSV files use a semicolon or a tab instead of a comma to separate values. For those, pass the separator: pd.read_csv("file.csv", sep=";"). If your columns all come in as one big column, a wrong separator is usually the reason.
๐พ Writing a DataFrame back to CSV
After you clean or change your data, you often want to save it. The reverse of read_csv is to_csv. You give it a file name and it writes the table out as text.
This saves the DataFrame to a new file:
import pandas as pd
df = pd.read_csv("people.csv")
# Add a column, then save the resultdf["bonus"] = df["salary"] * 0.1df.to_csv("people_with_bonus.csv", index=False)
print("Saved!")Output
Saved!The important part is index=False. By default Pandas writes the index (0, 1, 2, โฆ) as an extra first column in the file. Most of the time you do not want that clutter, so index=False leaves it out. The new file people_with_bonus.csv now holds your table plus the bonus column, ready to open in Excel or read again later.
๐ Quick reference
The CSV moves from this lesson in one place.
| What you want | The code |
|---|---|
| Read a CSV file | pd.read_csv("people.csv") |
| Use a column as the index | pd.read_csv("f.csv", index_col="name") |
| Read only some columns | pd.read_csv("f.csv", usecols=["name"]) |
| Read just the first rows | pd.read_csv("f.csv", nrows=3) |
| Different separator | pd.read_csv("f.csv", sep=";") |
| Save to CSV | df.to_csv("out.csv", index=False) |
โ ๏ธ Common Mistakes
A few things that trip people up:
- Wrong file path. If the file is not in the folder you run from, you get
FileNotFoundError. Put the file beside your script, or give the full path.
# โ Fails if people.csv is not in the current folderdf = pd.read_csv("people.csv") # FileNotFoundError
# โ
Give the full path when the file is elsewheredf = pd.read_csv("C:/data/people.csv")- Forgetting
index=Falsewhen saving. Without it, Pandas adds an extra unnamed column of row numbers to the file, which is messy and confusing later. - Wrong separator. If every value lands in one column, the file probably uses
;or a tab. Pass the rightsepto fix it.
โ Best Practices
Habits that make file loading painless:
- Always run
df.head()anddf.shaperight afterread_csv, so you confirm the data loaded the way you expected. - Keep your data files in a known folder and use clear paths, so you do not chase
FileNotFoundError. - Use
index=Falsewhen saving, unless you really want the row numbers in the file. - For a giant file, load
nrows=5first to check the columns before reading the whole thing.
๐งฉ What Youโve Learned
โ A CSV is a plain-text file where each line is a row and values are separated by commas, with the first line as the header.
โ
pd.read_csv("file.csv") loads the file into a DataFrame, using the first line as column names and guessing the types.
โ
Options like index_col, usecols, nrows, and sep handle the common variations between files.
โ
df.to_csv("out.csv", index=False) saves a DataFrame back to a CSV, and index=False keeps the row numbers out.
โ Once a file is a DataFrame, all the filtering and selecting from the last lesson works the same.
Check Your Knowledge
Test what you learned. Pick an answer for each question, then click Check.
- 1
What does pd.read_csv("people.csv") do by default with the first line of the file?
Why: By default the first line is the header, so Pandas uses those values as the column names and starts the data from the next line.
- 2
Which option makes read_csv use the 'name' column as the row labels?
Why: index_col="name" sets that column as the index, so the rows are labelled by name instead of 0, 1, 2.
- 3
Why do you usually pass index=False to df.to_csv?
Why: Without index=False, Pandas writes the index (0, 1, 2, ...) as an extra first column. index=False leaves that clutter out of the file.
- 4
Every value from your CSV lands in a single column. What is the likely cause?
Why: If columns are not splitting, the file probably uses a different separator. Pass the right one with sep, like sep=";".
๐ Whatโs Next?
You can now pull real data out of a file and into a DataFrame. But real data is rarely clean. It has blanks, duplicates, and odd values. Next we learn the basics of data cleaning, so your numbers can be trusted before you analyze them.