Concept 2.1: Filtering Data
Filtering Data in Pandas
Pandas provides powerful ways to filter data using boolean conditions. You can:
- Filter rows using boolean conditions
- Select specific columns
- Combine multiple conditions
Code Sample
import pandas as pd
# Sample DataFrame
df = pd.DataFrame({
'name': ['John', 'Anna', 'Peter'],
'age': [28, 22, 35],
'city': ['New York', 'Paris', 'London']
})
# Filter by condition
young_people = df[df['age'] < 30]
# Multiple conditions
young_ny = df[(df['age'] < 30) & (df['city'] == 'New York')]
Exercises
Exercise 2.1.1: Simple Filtering
Given a DataFrame 'df' with columns 'name', 'age', and 'city', write code to filter for people who are older than 25
Exercise 2.1.2: Complex Filtering
Given a DataFrame 'df' with columns 'name', 'age', and 'city', write code to filter for people who are older than 25 and live in New York