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
- π Technique Card: Reading CSV Files with Pandas (practical)
- π Technique Card: Pandas and Matplotlib Bar Charts (practical)
- π Technique Card: Understanding Pandas Series and Dataframes (deeper)
- π Technique Card: Working With CSV Files (practical)
- π Technique Card: Telling Stories With Data (deeper)