Skip to content

Filtering Data (Pandas)

🟨🐍 Technique Card: Filtering Data using Pandas (practical)

🧰 What You’ll Learn

How to filter a table of data (DataFrame) using Pandas so you can work with just the parts you care about β€” like countries in a certain continent.


🧠 Key Concept

Filtering means picking only the rows in a table (called a DataFrame) that match something β€” like rows where the β€œContinent” is β€œEurope”.


πŸ› οΈ How to Do It

import pandas as pd

# Read in the data
df = pd.read_csv("countries.csv")

# See what the table looks like
print(df.head())

# Filter rows where the continent is europe
europe_df = df[df["continent"] == "europe"]

# Show the first few rows of the result
print(europe_df.head())

πŸ—‚οΈ What’s Going On?

  • df["continent"] == "europe" checks each row to see if it's in europe.
  • df[...] gives you just the rows where that is True.
  • You can store this in a new DataFrame like europe_df.

🌏 You Could Try

βœ… Filtering by a different continent:

asia_df = df[df["continent"] == "asia"]

βœ… Filtering by more than one condition:

# countries in asia with gini less than 35
equal_asia_df = df[(df["continent"] == "asia") & (df["gini"] < 35)]

βœ… Using | for OR instead of &:

# countries in asia OR europe
asia_or_europe_df = df[(df["continent"] == "asia") | (df["continent"] == "europe")]

🎯 Challenge Ideas

  • Filter for countries with a GINI of more than 40 - what do you notice about them?
  • Find all the countries in a continent of your own choice.
  • Filter to see only countries in europe with a GINI of less than 30.

🧩 Vocabulary Boost

  • DataFrame – A table of data in Pandas.
  • Filter – Picking rows that match a condition.
  • Boolean – True or False β€” used in filtering.

πŸ”— Linked Cards


Watch the video


Watch the video