PYTHON FOR AI • LESSON 3

Reading CSV

CSV files are one of the most common ways to store tabular data. Pandas makes it very easy to load a CSV file into a DataFrame so we can inspect, clean, analyze, and prepare the data for Machine Learning.

CORE IDEA

Read a CSV file and turn it into a DataFrame.

A CSV file stores data in rows and columns. Pandas' read_csv() function reads that file and creates a DataFrame that we can work with using Python.

01

What Is a CSV File?

CSV stands for Comma-Separated Values.

It is a simple text file used to store tabular data. Each line normally represents one row, while commas separate the columns.

Name,Age,City
Raj,29,Hyderabad
John,32,London
Sarah,27,New York

This is a CSV file containing three columns: Name, Age, and City.

02

From CSV File to DataFrame

Before reading a CSV file, think about the process:

CSV File
   ↓
pd.read_csv()
   ↓
Pandas DataFrame
   ↓
Analyze / Clean / Prepare Data

This is one of the most important workflows in Pandas.

03

Example CSV File

Suppose we have a file called:

students.csv

Its contents are:

Name,Age,Score
Raj,29,85
John,32,90
Sarah,27,78
David,30,92

The first row contains the column names.

The remaining rows contain the actual data.

04

Read a CSV File

Pandas provides the read_csv() function for reading CSV files.

import pandas as pd

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

print(df)
    Name  Age  Score
0    Raj   29     85
1   John   32     90
2  Sarah   27     78
3  David   30     92

The CSV file has now been converted into a Pandas DataFrame stored inside the variable df.

05

Understand the Code

Step 1 — Import Pandas

import pandas as pd

This imports the Pandas library and gives it the shorter name pd.

Step 2 — Read the CSV

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

Pandas opens students.csv, reads its rows and columns, and creates a DataFrame.

Step 3 — Store the DataFrame

df = ...

The DataFrame is stored in the variable df.

06

Check the First Rows

After loading a dataset, you should not immediately start modifying it.

First, inspect what you actually loaded.

print(df.head())
    Name  Age  Score
0    Raj   29     85
1   John   32     90
2  Sarah   27     78
3  David   30     92

head() displays the first five rows by default.

07

Check the Last Rows

You can use tail() to inspect the bottom of the dataset.

print(df.tail())

You can also specify how many rows you want.

print(df.tail(2))
    Name  Age  Score
2  Sarah   27     78
3  David   30     92
08

Check the Dataset Size

The shape property tells us how many rows and columns the DataFrame contains.

print(df.shape)
(4, 3)

This means:

  • 4 rows
  • 3 columns

The order is always:

(rows, columns)
09

Check Column Names

Use columns to see the names of all columns.

print(df.columns)
Index(['Name', 'Age', 'Score'], dtype='object')

This is useful when you are working with a large dataset and do not remember the exact column names.

10

Check Data Types

Different columns may contain different types of data.

print(df.dtypes)
Name     object
Age       int64
Score     int64
dtype: object

Here:

  • Name contains text.
  • Age contains integers.
  • Score contains integers.

Checking data types is important because Machine Learning algorithms need data in appropriate formats.

11

Read a CSV From a Different Folder

The CSV file does not have to be in the same directory as your Python file.

For example:

project/
│
├── data/
│   └── students.csv
│
└── main.py

From main.py, you can read the file using:

import pandas as pd

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

print(df)

The path tells Python where the CSV file is located.

12

Using an Absolute Path

You can also provide the complete path to a file.

df = pd.read_csv(
    "/home/user/project/data/students.csv"
)

However, relative paths are usually more convenient when your project has a well-organized folder structure.

13

CSV Without Column Names

Not every CSV file contains a header row.

For example:

Raj,29,85
John,32,90
Sarah,27,78

If the CSV does not contain column names, you can tell Pandas that there is no header.

df = pd.read_csv(
    "students.csv",
    header=None
)

print(df)
       0   1   2
0    Raj  29  85
1   John  32  90
2  Sarah  27  78

Pandas automatically creates numeric column names: 0, 1, and 2.

14

Give Columns Their Own Names

If the CSV has no header, we can provide our own column names.

df = pd.read_csv(
    "students.csv",
    header=None,
    names=["Name", "Age", "Score"]
)

print(df)
    Name  Age  Score
0    Raj   29     85
1   John   32     90
2  Sarah   27     78
15

Real-World AI Example

Imagine you are building a Machine Learning model that predicts house prices.

Your company gives you a CSV file:

houses.csv

The file contains:

Area,Bedrooms,Age,Price
1200,3,10,250000
1800,4,5,400000
900,2,20,180000
2200,4,3,500000

You can load it into Pandas:

import pandas as pd

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

print(df.head())

Now the CSV data is inside a DataFrame and you can inspect and prepare it before using it for Machine Learning.

16

A Good Workflow After Reading CSV

Reading the file is only the beginning. A practical workflow is:

import pandas as pd

# 1. Read the data
df = pd.read_csv("houses.csv")

# 2. Look at the data
print(df.head())

# 3. Check size
print(df.shape)

# 4. Check columns
print(df.columns)

# 5. Check data types
print(df.dtypes)

After this, you can start filtering, sorting, handling missing values, and grouping the data.

KEY TAKEAWAY

read_csv() turns CSV data into a DataFrame.

CSV files are commonly used to store real-world datasets. Pandas provides pd.read_csv() to load those files into a DataFrame. After loading the data, inspect it using methods and properties such as head(), tail(), shape, columns, and dtypes. In AI and Machine Learning, reading the dataset is the first step before cleaning, analyzing, and preparing the data for a model.