Reading CSV Files
Learn how to load a CSV file into a Pandas DataFrame in Google Colab and on your own computer, so you can start exploring real datasets.
Skill Level: Beginner
Prerequisites: Introduction to Pandas, Installing and Importing Libraries
Estimated Time: 25 minutes
Story Time
You now know how to use Pandas and how to import libraries.
Imagine you are given a file containing exam scores for an entire class. The file is in CSV format, and you want to explore the data in Python instead of scrolling through rows in a spreadsheet.
In this lesson, you will learn how to load that CSV file into a Pandas DataFrame so you can start your analysis. You’ll do this first in Google Colab, and then see what changes when you work in VS Code or Jupyter on your own computer.
What You’ll Learn
By the end of this lesson, you will be able to:
- Explain what a CSV file is.
- Upload and read a CSV file in Google Colab.
- Load a CSV file using
pd.read_csv()into a DataFrame. - Display the first and last few rows of a dataset.
- Understand basic file paths (including relative paths) when working locally.
- Recognize common errors when loading CSV files.
Why This Topic Matters
Most real-world datasets are stored as files.
Before you can clean, analyze, or visualize data, you first need to load them into Python. One of the most common formats you will encounter is CSV (Comma-Separated Values), because it is simple and widely supported.
Understanding how to read CSV files—both in Colab and on your own machine—is an essential first step in working with real datasets.
What Is a CSV File?
A CSV file stores data in rows and columns.
- Each row represents one record.
- Each column represents one attribute or variable.
Example:
| Name | Age | GPA |
|---|---|---|
| Emma | 21 | 3.8 |
| Alex | 22 | 3.5 |
| Sophia | 20 | 3.9 |
CSV files are popular because they are lightweight, easy to create, and compatible with many tools including spreadsheets, databases, and programming languages.
Our Dataset
Throughout this module, you will use the Students Performance in Exams dataset.[file:42]
It contains information such as:
- Gender
- Race/Ethnicity
- Parental level of education
- Lunch type
- Test preparation course
- Math score
- Reading score
- Writing score
Your goal in this lesson is to load this dataset into Python so that you can begin exploring it with Pandas.
Getting the File into Google Colab
In Colab, your notebook runs in the cloud, so you need to upload the CSV file or access it from Google Drive.
Option 1: Upload from your computer
-
In your Colab notebook, run:
from google.colab import filesuploaded = files.upload() -
Use the file picker to upload
StudentsPerformance.csv. -
After upload, the file will be available in the notebook’s working directory.
-
Then you can read it with:
import pandas as pddf = pd.read_csv("StudentsPerformance.csv")
Option 2: Use Google Drive (optional for later)
If you store the file in Google Drive:
-
Mount your drive in Colab:
from google.colab import drivedrive.mount('/content/drive') -
Use the path to the file in your Drive folder, for example:
import pandas as pddf = pd.read_csv("/content/drive/MyDrive/Data/StudentsPerformance.csv")
For this course, the simplest path is usually upload from your computer.
Reading a CSV File with Pandas
Once the file is available, use the read_csv() function from the Pandas library:
import pandas as pd
df = pd.read_csv("StudentsPerformance.csv")
Here:
pdrefers to the Pandas library.read_csv()reads the file into memory.dfis a DataFrame that contains the dataset.
Viewing the First Few Rows
Use the head() function to preview the dataset:
df.head()
By default, head() displays the first five rows.
This is useful for quickly checking that the file loaded correctly and that the columns look as expected.
Viewing More Rows
You can ask for more rows by passing a number to head():
df.head(10)
This displays the first ten rows of the DataFrame.
Viewing the Last Rows
To see the end of the dataset, use the tail() function:
df.tail()
By default, tail() displays the last five rows.
This can help you check if the dataset ends where you expect and whether any unusual values appear at the end.
Understanding the DataFrame
Once the CSV file is loaded, it becomes a DataFrame inside Python.
You can think of a DataFrame as a spreadsheet living in your program:
- Each row represents one observation.
- Each column represents one variable.
You will learn more about DataFrames in later lessons, but for now it is enough to understand that df is your in‑memory table.
Working Locally: File Paths and Relative Paths
When you run Python in VS Code or Jupyter on your own computer, pd.read_csv() needs to know where the file is on your machine.
-
A relative path is based on the location of your Python file or notebook.
-
If your notebook and CSV are in the same folder:
df = pd.read_csv("StudentsPerformance.csv") -
If the CSV is in a subfolder called
data:df = pd.read_csv("data/StudentsPerformance.csv")
-
-
An absolute path includes the full location on your system, for example:
df = pd.read_csv("C:/Users/YourName/projects/data/StudentsPerformance.csv")
For most projects, relative paths are preferred because they make your code easier to run on different machines and environments.
Worked Example
import pandas as pd
df = pd.read_csv("StudentsPerformance.csv")
df.head()
As you look at the output, consider:
- How many columns do you see?
- What information does each column contain?
- Which columns appear to contain numerical data?
- Which columns contain text?
These questions help you start understanding the structure of the dataset.[file:42]
Practice in the Notebook
After completing the activity in Google Colab, mark it as complete below.
In Google Colab:
- Upload the StudentsPerformance dataset (
StudentsPerformance.csv). - Import Pandas.
- Read the dataset into a DataFrame named
students. - Display the first five rows using
students.head(). - Display the last five rows using
students.tail().
Then try:
students.head(10)
students.tail(10)
and notice how the number of rows changes.
Independent Exercise
Suppose a colleague gives you a CSV file named employee_data.csv.
Without referring to your notes, write the Python code needed to:
- Import Pandas
- Read the CSV file
- Store it in a DataFrame named
employees - Display the first five rows
Try typing the code directly in a notebook (Colab or VS Code) and run it to confirm it works.
Common Mistakes
Forgetting to Import Pandas
Incorrect:
df = pd.read_csv("StudentsPerformance.csv")
This will cause an error because pd is not defined.
Correct:
import pandas as pd
df = pd.read_csv("StudentsPerformance.csv")
Incorrect File Name or Path
pd.read_csv("student.csv")
If the file name or path is wrong, Python will raise a FileNotFoundError. Always check:
- The exact file name (including capitals).
- The folder where the file is stored.
- Whether you are using the right relative path.
Forgetting Quotation Marks
Incorrect:
pd.read_csv(StudentsPerformance.csv)
Correct:
pd.read_csv("StudentsPerformance.csv")
File names must be provided as strings, enclosed in quotes.
Key Takeaways
In this lesson, you learned that:
- A CSV file stores data in rows and columns.
- In Colab, you can upload files or read them from Google Drive, then use
pd.read_csv()to load them. - The loaded data is stored in a DataFrame.
head()andtail()help you preview the beginning and end of a dataset.- Relative paths are used to locate files when working in VS Code or Jupyter on your own machine.
Self Evaluation
Check Your Understanding
1. What is a CSV file?
2. Which Pandas function is used to read a CSV file?
3. After reading a CSV file with pd.read_csv(), what type of object is created?
4. What does df.head() do?
5. Which error is likely if the file name is incorrect when calling pd.read_csv()?
Continue Your Journey
Next, you will learn how to read Excel files, so you can work with datasets stored in spreadsheet formats as well.