Introduction to Python for Data Analysis

Filtering Rows in DataFrames

Learn how to filter rows in Python DataFrames using the pandas library.

Course Sections
Goals In this section, you will learn how to:
  • Filter rows in a DataFrame based on conditions
  • Use boolean indexing to select specific rows
  • Combine multiple conditions for complex filtering

Sample DataFrame

Let’s start with a sample DataFrame that we’ll use throughout this section:

idnameagecitysalary
1Alice25New York50000
2Bob30San Francisco75000
3Carol35Chicago60000
4David40New York80000
5Emily28Chicago55000

Filtering Rows

Pandas provides powerful ways to filter rows based on conditions. We use boolean indexing to select rows that meet specific criteria.

Syntax

 # Basic filtering
filtered_df = df[df['column_name'] condition]
 
# Multiple conditions
filtered_df = df[(condition1) & (condition2)] 

Exercise 2.1: Basic Filtering (Numbers)

Run the code below to filter the DataFrame and show only employees who are older than 30:

Filter employees older than 30

Exercise 2.2: Basic Filtering (Strings)

Modify the code below to filter the DataFrame and show only employees from Chicago:

Filter employees from Chicago

Exercise 2.3: Filtering with AND (&)

Modify the code below to filter the DataFrame and show employees from New York with a salary greater than 60000:

Filter New York employees with high salary

Exercise 2.4: Filtering with OR (|)

Modify the code below to filter the DataFrame and show employees who are either from Chicago or have a salary less than 70000:

Filter employees from Chicago or with salary less than 70000

Exercise 2.5: Complex Filtering (Combining AND and OR)

Modify the code below to filter the DataFrame and show employees who are either from New York and older than 30, or have a salary greater than 70000:

Complex filtering

Quiz

Loading...

Loading...

summary We've learned how to:
  • Filter rows in a DataFrame using boolean indexing
  • Use comparison operators ( > , < , == , etc.) for filtering
  • Combine multiple conditions using & (and) and | (or) operators
  • Create complex filters to select specific subsets of data