VOOZH about

URL: https://www.geeksforgeeks.org/pandas/how-to-select-rows-from-a-dataframe-based-on-column-values/

⇱ How to Select Rows from a Dataframe based on Column Values ? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Select Rows from a Dataframe based on Column Values ?

Last Updated : 15 Jul, 2025

Selecting rows from a Pandas DataFrame based on column values is a fundamental operation in data analysis using pandas. The process allows to filter data, making it easier to perform analyses or visualizations on specific subsets. Key takeaway is that pandas provides several methods to achieve this, each suited to different scenarios. Let's start with a quick example using boolean indexing - commonly used method in Pandas for row selection: 

Output:

👁 Select-rows-from-a-dataframe-based-on-column-values
Select rows from a dataframe based on column values

In this example, we created a DataFrame and selected rows where age is greater than 25. This simple operation showcases power of pandas in filtering data efficiently.

Method 1. loc Method for Conditional Row Selection

The loc method is significant because it allows you to select rows based on labels and conditions. It is particularly useful when you need to filter data using specific criteria, such as selecting rows where a column value meets a certain condition. This method enhances readability and maintains the logical flow of data manipulation.


Output
 Name Age City
2 Charlie 22 Chicago

Method 2: Using Boolean Indexing for Complex Conditions

Boolean indexing is significant because it enables complex filtering operations by combining multiple conditions. This method allows for expressive and flexible data selection, making it possible to filter data with intricate criteria using logical operators.


Output
Empty DataFrame
Columns: [Name, Age, City]
Index: []

Method 3. query Method for SQL-Like Queries

The query method is significant because it provides an SQL-like syntax for filtering DataFrames. This method can be more intuitive for users familiar with SQL, allowing them to write queries in a familiar format while leveraging pandas' capabilities.


Output
 Name Age City
0 Alice 24 New York
1 Bob 27 Los Angeles
2 Charlie 22 Chicago

Method 4: Using isin Method for Membership-Based Selection

The isin method is significant because it allows you to filter rows based on membership within a list of values. This method is particularly useful when you want to select rows that match any of several values in a column, enhancing flexibility in data selection.


Output
 Name Age City
0 Alice 24 New York
2 Charlie 22 Chicago
Comment

Explore