Python Lesson 8: pandas — Reading and Exploring Data

📎 This article includes 3 downloadable practice files ↓

⏱ 2 min read

📘 Python Beginner Course · Lesson 8 of 12

In this article
  1. Read data
  2. Look before you leap
  3. Calculated columns
  4. Picking columns and rows
  5. Quick answers
  6. Where beginners go wrong
  7. Practice

pandas is the reason so many analysts learn Python. A DataFrame is a table with named columns, like an Excel Table, and operations on whole columns take one line.

Read data

import pandas as pd
df = pd.read_csv("sales.csv", parse_dates=["date"])
# Excel: pd.read_excel("file.xlsx", sheet_name="Data")   (needs openpyxl)

Look before you leap

df.shape          # (120, 7)  rows, columns
df.dtypes         # the type of each column
df.head(3)        # first 3 rows   (df.tail() for the last)
df.info()         # columns, non-null counts, types
df.describe()     # count, mean, min, max of number columns
💡 Check dtypes first. If ‘qty’ shows as object (text), something in the column isn’t a number, and sums will fail or join text.

Calculated columns

df["amount"] = df["qty"] * df["rate"]
df["month"] = df["date"].dt.month

No filling down: the calculation applies to every row at once.

Picking columns and rows

df["customer"]                         # one column (a Series)
df[["customer", "amount"]]             # several columns (a DataFrame)
df[df["product"] == "Laptop"]          # rows where product is Laptop
df[(df["amount"] > 100000) & (df["region"] == "North")]   # AND: &, OR: |, brackets required
df[df["region"].isin(["North", "East"])]
df.sort_values("amount", ascending=False).head(5)          # top 5 orders

Quick answers

df["amount"].sum()
(df["product"] == "Laptop").sum()      # count of True values
df["customer"].nunique()               # distinct customers
df["region"].value_counts()            # rows per region

Where beginners go wrong

Error Fix
Using and/or between conditions Use & and |, each condition in brackets
KeyError: ‘Amount’ Column names are case-sensitive; check df.columns
SettingWithCopyWarning Filter into a new variable with .copy() before changing it
Dates as text parse_dates=[…] or pd.to_datetime()

Practice

Download the script (and data file, if listed) below into one folder. Open a terminal in that folder and run python lesson-NN.py. Compare with the expected output, then change something and run it again: that’s how Python is learned.

📎 Practice files for this article

  • 🐍
    Lesson 8 scriptThe complete script from this lesson, tested.
    ⬇ PY · 497 B
  • 📄
    Expected outputWhat the script prints when you run it.
    ⬇ TXT · 522 B
  • 🧾
    sales.csv120 orders (July-September 2026) used by the scripts.
    ⬇ CSV · 7 KB

Free to use for learning. Files with macros (.bas) are plain text — import them with Alt+F11 → File → Import File, and always test on a copy.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free · AI can be wrong