Basic Filtering
Filter rows based on conditions:
import pandas as pd
df = pd.DataFrame({
'name': ['Alice', 'Bob', 'Charlie', 'David'],
'age': [25, 30, 35, 40],
'city': ['NYC', 'LA', 'NYC', 'Chicago']
})
# Single condition
adults = df[df['age'] >= 30]
# Returns rows where age >= 30
# Multiple conditions (use & for AND, | for OR)
nyc_adults = df[(df['age'] >= 30) & (df['city'] == 'NYC')]
young_or_nyc = df[(df['age'] < 30) | (df['city'] == 'NYC')]
Using query() Method
More readable filtering:
# Using query()
adults = df.query('age >= 30')
nyc_people = df.query("city == 'NYC'")
# Multiple conditions
result = df.query('age >= 30 and city == "NYC"')
result = df.query('age < 30 or city == "NYC"')
Filtering Columns
Select specific columns:
# Single column
ages = df['age']
# Multiple columns
subset = df[['name', 'age']]
# Columns by condition
numeric_cols = df.select_dtypes(include=['number'])
String Filtering
Filter based on string conditions:
# Contains
contains_a = df[df['name'].str.contains('a', case=False)]
# Starts with
starts_with_a = df[df['name'].str.startswith('A')]
# Ends with
ends_with_e = df[df['name'].str.endswith('e')]
# Exact match
exact = df[df['name'] == 'Alice']
isin() Method
Filter by list of values:
# Filter by multiple values
cities = ['NYC', 'LA']
filtered = df[df['city'].isin(cities)]
# Not in list
excluded = df[~df['city'].isin(['Chicago'])]
Filtering by Index
Filter using index labels:
# By label
row = df.loc[0] # First row
# By position
row = df.iloc[0] # First row
# Multiple rows
rows = df.iloc[0:3] # First 3 rows
Combining Filters
Combine multiple filtering operations:
# Chain filters
result = df[df['age'] >= 30][df['city'] == 'NYC']
# Or use & operator (better)
result = df[(df['age'] >= 30) & (df['city'] == 'NYC')]
# Complex conditions
result = df[
(df['age'] >= 25) &
(df['age'] <= 35) &
(df['city'].isin(['NYC', 'LA']))
]
Best Practices
- Use & and |: Not 'and' and 'or' for boolean operations
- Parentheses required: Wrap each condition in parentheses
- Use query() for readability: Especially for complex conditions
- Consider performance: For large DataFrames, use vectorized operations
Next Steps
Learn about GroupBy for aggregating filtered data, or explore DataFrames for more operations.