ZMedia Purwodadi

How to Read a CSV File in Python (Step-by-Step for Beginners in Nigeria)

Table of Contents

Why Reading CSV Files Is Your First Real Python Skill

You've installed Python. You've run your first "Hello World." Now what?

Here's the honest truth: most beginner Python tutorials teach you theory for weeks before you touch any real data. That's backwards. In Nigeria's job market and in freelance data work employers want to see that you can open a dataset and start working with it. Fast.

The good news? Reading a CSV file in Python takes literally three lines of code. This tutorial will show you exactly how to do it, explain what each line means, and give you a real-world exercise using Nigerian data so you can practise right now.

How to Read a CSV File in Python (Step-by-Step for Beginners in Nigeria)

What Is a CSV File?

CSV stands for Comma-Separated Values. It's one of the most common data formats in the world. When you download data from any Nigerian government portal, export transactions from your bank, or pull sales data from a spreadsheet, there's a very good chance it comes as a CSV file.

A CSV file looks like this if you open it in Notepad:

Name,State,Score
Adeola,Lagos,87
Emeka,Anambra,92
Fatima,Kano,78

Each row is a record. Each column is separated by a comma. Simple as that.

What You Need Before You Start

  • Python installed on your computer (see our beginner guide)
  • Jupyter Notebook open and ready (how to install Jupyter Notebook)
  • The pandas library installed (run pip install pandas in your terminal if you haven't)
  • A CSV file to practise with (we'll create one below)

Step 1: Create a Sample CSV File

Open Notepad (Windows) or TextEdit (Mac), paste the following, and save it as students.csv on your Desktop:

Name,State,Course,Score
Adeola Bello,Lagos,Data Analysis,87
Emeka Okonkwo,Anambra,Python Programming,92
Fatima Aliyu,Kano,Data Analysis,78
Chidi Nwosu,Rivers,Python Programming,85
Ngozi Eze,Enugu,Data Analysis,91

Step 2: Open Jupyter Notebook and Read the File

Open Jupyter Notebook and create a new notebook. In the first cell, type this:

import pandas as pd

df = pd.read_csv("students.csv")

print(df)

Run the cell (press Shift + Enter). You should see a clean table with all five students displayed.

That's it. Three lines of code, and you've loaded your first dataset.

What Each Line Does

Line 1: import pandas as pd
This loads the pandas library into your notebook. Pandas is Python's most important tool for working with data. The as pd part just means you can type pd instead of pandas every time it's a shortcut.

Line 2: df = pd.read_csv("students.csv")
This reads your CSV file and stores it in a variable called df. The name df stands for DataFrame, which is what pandas calls a table of data. You'll see this abbreviation everywhere in data science.

Line 3: print(df)
This displays the DataFrame in your notebook. In Jupyter specifically, you can also just type df (without print) on its own line and it will display in a nicely formatted table.

Useful Things to Do After Reading a CSV

Once your data is loaded, here are the first commands every data analyst runs:

# See the first 5 rows
df.head()

# See the shape how many rows and columns
df.shape

# See column names
df.columns

# See basic statistics
df.describe()

# Check for missing values
df.isnull().sum()

Try running each of these in a separate cell in Jupyter. Each one tells you something important about your data.

What If the File Is Not in the Same Folder?

If you saved your CSV to a different folder, you'll need to include the full file path. On Windows, it looks like this:

df = pd.read_csv("C:/Users/YourName/Desktop/students.csv")

On Mac or Linux:

df = pd.read_csv("/Users/YourName/Desktop/students.csv")

Pro tip: The easiest approach when starting out is to save your CSV file in the same folder where your Jupyter Notebook is saved. Then you only need the file name, no full path needed.

Practice Challenge

Download any free Nigerian dataset for example, state-level population data or JAMB result data and try reading it in Jupyter. Then answer these questions using the commands above:

  1. How many rows and columns does the dataset have?
  2. What are the column names?
  3. Are there any missing values?

If you get stuck, drop your question in the comments below or join our Python Learners WhatsApp Community for direct help.

What's Next

Reading a CSV is just the beginning. In the next post in this series, we'll cover how to clean messy data in Python handling missing values, fixing wrong data types, and renaming columns. That's where the real data analysis work starts.

If you found this tutorial helpful, share it with a friend who's learning Python in Nigeria. The more people who build these skills, the stronger our tech community gets.

Jacob Isah is the founder of JacobIsah Programming Hub and NEXODE Academy, helping Nigerian learners build practical tech skills from beginner to builder. Want structured, hands-on Python exercises? Check out the Python Exercise Library on Selar.

Post a Comment