Loading and Exploring Data
Real data usually comes in a file. The first job is to load it into pandas and get to know it.
The Example File
In this chapter we use sales.csv, the sales of a bakery with three branches. A CSV (comma separated values) file is plain text: one row per line, with commas between the values.
Example
date,branch,item,quantity,price
2026-10-01,Saddar,Cake,3,1200
2026-10-01,Orangi,Bread,10,150
2026-10-02,Gulshan,Cake,2,1200
2026-10-02,Saddar,Biscuits,8,250
2026-10-03,Orangi,Cake,4,1200
2026-10-03,Gulshan,Bread,12,150
2026-10-04,Saddar,Bread,6,150
2026-10-04,Orangi,Biscuits,5,250Save these lines in a file called sales.csv in the same folder as your notebook to follow along.
Reading a CSV File
Example
import pandas as pd
df = pd.read_csv("sales.csv")
print(df.head(3))
print(df.shape)date branch item quantity price 0 2026-10-01 Saddar Cake 3 1200 1 2026-10-01 Orangi Bread 10 150 2 2026-10-02 Gulshan Cake 2 1200 (8, 5)
head(3)shows the first 3 rows (5 if you leave the number out)shapeshows 8 rows and 5 columns
tail() shows the last rows:
Example
print(df.tail(2))date branch item quantity price 6 2026-10-04 Saddar Bread 6 150 7 2026-10-04 Orangi Biscuits 5 250
Other File Types
| File | Function |
|---|---|
| CSV | pd.read_csv("file.csv") |
| Excel | pd.read_excel("file.xlsx", sheet_name="Sheet1") |
| JSON | pd.read_json("file.json") |
| From a web link | pd.read_csv("https://.../file.csv") |
| SQL database | pd.read_sql(query, connection) |
Reading Excel files needs one extra library: pip install openpyxl.
Useful read_csv options: sep=";" for files that use semicolons, encoding="utf-8" for Urdu or other non-English text, and parse_dates=["date"] to read a column as dates.
Column Types
dtypes tells you the type of each column. Numbers must have a number type, or maths will not work:
Example
print(df[["quantity", "price"]].dtypes)quantity int64 price int64 dtype: object
df.info() gives a complete summary in one call: the number of rows, each column's type, and how many values are not empty. It is the best first command on any new dataset.
Quick Statistics
describe() summarises all number columns:
Example
print(df.describe())quantity price count 8.000000 8.000000 mean 6.250000 568.750000 std 3.494894 524.361857 min 2.000000 150.000000 25% 3.750000 150.000000 50% 5.500000 250.000000 75% 8.500000 1200.000000 max 12.000000 1200.000000
| Row | Meaning |
|---|---|
| count | Number of values that are not empty |
| mean | The average |
| std | Standard deviation: how spread out the values are |
| min / max | Smallest and largest value |
| 25%, 50%, 75% | Percentiles; 50% is the median (middle value) |
Exploring Text Columns
How many sales did each branch make, and which items exist?
Example
print(df["branch"].value_counts())
print(df["item"].unique().tolist())branch Saddar 3 Orangi 3 Gulshan 2 Name: count, dtype: int64 ['Cake', 'Bread', 'Biscuits']
Single numbers are easy too:
Example
print("Total items sold:", df["quantity"].sum())
print("Average price:", df["price"].mean())Total items sold: 50 Average price: 568.75
A First Exploration Checklist
df.head(): does the data look right?df.shape: how many rows and columns?df.info(): are the types correct? Are values missing?df.describe(): are the numbers sensible? (A negative price means a mistake.)value_counts()on text columns: any spelling differences, like "Karachi" and "karachi"?
Exercise
Exercise
Which pandas function loads a CSV file into a DataFrame?
