Python Reading CSV Files with Pandas

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,salary
Alex,25,London,50000
Riya,30,Mumbai,60000
Arjun,22,Toronto,45000
Maria,35,London,80000
Sam,28,Mumbai,52000

Read 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 salary
0 Alex 25 London 50000
1 Riya 30 Mumbai 60000
2 Arjun 22 Toronto 45000
3 Maria 35 London 80000
4 Sam 28 Mumbai 52000

That 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 salary
1 Riya 30 Mumbai 60000
3 Maria 35 London 80000

Caution

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 index
df = pd.read_csv("people.csv", index_col="name")
# Read only some of the columns
df2 = pd.read_csv("people.csv", usecols=["name", "salary"])
# Load just the first 3 rows, handy for a peek at a huge file
df3 = pd.read_csv("people.csv", nrows=3)
print(df.head(2))

Output

age city salary
name
Alex 25 London 50000
Riya 30 Mumbai 60000

What 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=3 reads 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 result
df["bonus"] = df["salary"] * 0.1
df.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 folder
df = pd.read_csv("people.csv") # FileNotFoundError
# โœ… Give the full path when the file is elsewhere
df = pd.read_csv("C:/data/people.csv")
  • Forgetting index=False when 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 right sep to fix it.

โœ… Best Practices

Habits that make file loading painless:

  • Always run df.head() and df.shape right after read_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=False when saving, unless you really want the row numbers in the file.
  • For a giant file, load nrows=5 first 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. 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. 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. 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. 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.

Data Cleaning Basics

Share & Connect