Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Data preprocessing
  • Data Manipulation
  • Data Analysis using Pandas
  • EDA
  • Pandas Exercise
  • Pandas AI
  • Numpy
  • Matplotlib
  • Plotly
  • Data Analysis
  • Machine Learning
  • Data science
Open In App
Next Article:
Filtering rows based on column values in PySpark dataframe
Next article icon

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 


Next Article
Filtering rows based on column values in PySpark dataframe

E

erakshaya485
Improve
Article Tags :
  • AI-ML-DS
  • Pandas
  • Python
  • AI-ML-DS With Python
  • Python pandas-dataFrame
  • Python-pandas
Practice Tags :
  • python

Similar Reads

  • Sort Rows or Columns in Pandas Dataframe Based on Values
    Sorting is a fundamental operation when working with data in Pandas. Whether you need to arrange rows in ascending or descending order or reorder columns based on values, Pandas provides powerful functions to make sorting easy and efficient. In this article, we'll explore different ways to sort rows
    4 min read
  • How to Drop rows in DataFrame by conditions on column values?
    In this article, we are going to see several examples of how to drop rows from the dataframe based on certain conditions applied on a column. Pandas provide data analysts a way to delete and filter data frame using dataframe.drop() method. We can use this method to drop such rows that do not satisfy
    3 min read
  • How to Select Rows from Pandas DataFrame?
    pandas.DataFrame.loc is a function used to select rows from Pandas DataFrame based on the condition provided. In this article, let's learn to select the rows from Pandas DataFrame based on some conditions. Syntax: df.loc[df['cname'] 'condition'] Parameters: df: represents data frame cname: represent
    2 min read
  • Filtering rows based on column values in PySpark dataframe
    In this article, we are going to filter the rows based on column values in PySpark dataframe. Creating Dataframe for demonstration:[GFGTABS] Python3 # importing module import spark # importing sparksession from pyspark.sql module from pyspark.sql import SparkSession # creating sparksession and givin
    2 min read
  • How to select a range of rows from a dataframe in PySpark ?
    In this article, we are going to select a range of rows from a PySpark dataframe. It can be done in these ways: Using filter().Using where().Using SQL expression. Creating Dataframe for demonstration: [GFGTABS] Python3 # importing module import pyspark # importing sparksession from pyspark.sql modul
    3 min read
  • Find duplicate rows in a Dataframe based on all or selected columns
    Duplicating rows in a DataFrame involves creating identical copies of existing rows within a tabular data structure, such as a pandas DataFrame, based on specified conditions or across all columns. This process allows for the replication of data to meet specific analytical or processing requirements
    5 min read
  • Select Rows From List of Values in Pandas DataFrame
    Let's learn how to select rows from a list of values in Pandas DataFrame using isin() method. Using isin() to Select Rows from a List of ValuesThe isin() function is one of the most commonly used methods for filtering data based on a list of values. Let’s walk through a simple example to illustrate
    4 min read
  • How to Select Single Column of a Pandas Dataframe
    In Pandas, a DataFrame is like a table with rows and columns. Sometimes, we need to extract a single column to analyze or modify specific data. This helps in tasks like filtering, calculations or visualizations. When we select a column, it becomes a Pandas Series, a one-dimensional data structure th
    2 min read
  • Pandas filter a dataframe by the sum of rows or columns
    In this article, we will see how to filter a Pandas DataFrame by the sum of rows or columns. This can be useful in some conditions. Let's suppose you have a data frame consisting of customers and their purchased fruits.  The rows consist of different customers and columns contain different types of
    4 min read
  • Split dataframe in Pandas based on values in multiple columns
    In this article, we are going to see how to divide a dataframe by various methods and based on various parameters using Python. To divide a dataframe into two or more separate dataframes based on the values present in the column we first create a data frame. Creating a DataFrame for demonestration[G
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences