Open In App

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

Last Updated : 29 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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: 

Python
import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],'Age': [24, 27, 22, 32],'City': ['New York', 'Los Angeles', 'Chicago', 'Houston']}
df = pd.DataFrame(data)

# Select rows where Age is greater than 25
selected_rows = df[df['Age'] > 25]
print(selected_rows)

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.

Python
import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],'Age': [24, 27, 22, 32],'City': ['New York', 'Los Angeles', 'Chicago', 'Houston']}
df = pd.DataFrame(data)

# Select rows where City is 'Chicago'
chicago_rows = df.loc[df['City'] == 'Chicago']
print(chicago_rows)

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.

Python
import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],'Age': [24, 27, 22, 32],'City': ['New York', 'Los Angeles', 'Chicago', 'Houston']}
df = pd.DataFrame(data)

# Select rows where Age is greater than 25 and City is 'New York'
complex_condition = df[(df['Age'] > 25) & (df['City'] == 'New York')]
print(complex_condition)

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.

Python
import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],'Age': [24, 27, 22, 32],'City': ['New York', 'Los Angeles', 'Chicago', 'Houston']}
df = pd.DataFrame(data)

# Use query to select rows where Age is less than 30
young_people = df.query('Age < 30')
print(young_people)

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.

Python
import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie', 'David'],'Age': [24, 27, 22, 32],'City': ['New York', 'Los Angeles', 'Chicago', 'Houston']}
df = pd.DataFrame(data)

# Select rows where City is either 'New York' or 'Chicago'
cities = df[df['City'].isin(['New York', 'Chicago'])]
print(cities)

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


Similar Reads