Data Science Tutorial menu

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

text
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,250

Save these lines in a file called sales.csv in the same folder as your notebook to follow along.

Reading a CSV File

Example

python
import pandas as pd

df = pd.read_csv("sales.csv")
print(df.head(3))
print(df.shape)
Output
         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)
  • shape shows 8 rows and 5 columns

tail() shows the last rows:

Example

python
print(df.tail(2))
Output
         date  branch      item  quantity  price
6  2026-10-04  Saddar     Bread         6    150
7  2026-10-04  Orangi  Biscuits         5    250

Other File Types

FileFunction
CSVpd.read_csv("file.csv")
Excelpd.read_excel("file.xlsx", sheet_name="Sheet1")
JSONpd.read_json("file.json")
From a web linkpd.read_csv("https://.../file.csv")
SQL databasepd.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

python
print(df[["quantity", "price"]].dtypes)
Output
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

python
print(df.describe())
Output
        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
RowMeaning
countNumber of values that are not empty
meanThe average
stdStandard deviation: how spread out the values are
min / maxSmallest 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

python
print(df["branch"].value_counts())
print(df["item"].unique().tolist())
Output
branch
Saddar     3
Orangi     3
Gulshan    2
Name: count, dtype: int64
['Cake', 'Bread', 'Biscuits']

Single numbers are easy too:

Example

python
print("Total items sold:", df["quantity"].sum())
print("Average price:", df["price"].mean())
Output
Total items sold: 50
Average price: 568.75

A First Exploration Checklist

  1. df.head(): does the data look right?
  2. df.shape: how many rows and columns?
  3. df.info(): are the types correct? Are values missing?
  4. df.describe(): are the numbers sensible? (A negative price means a mistake.)
  5. value_counts() on text columns: any spelling differences, like "Karachi" and "karachi"?

Exercise

Exercise

Which pandas function loads a CSV file into a DataFrame?

Learn Data Science with a teacher

Join the Python Programming course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp