

Pandas Tutorial Part #9 – Filter DataFrame Rows
source link: https://thispointer.com/pandas-tutorial-part-9-filter-dataframe-rows/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

This tutorial will explain how to select rows from a DataFrame based on conditions.
Table of Contents
Select DataFrame rows based on conditions
We can select only those rows from a DataFrame that satisfies a condition. For example, suppose we have DataFrame like this,
Name Product Sale 0 Mark Apples 44 1 Aadi Mangos 31 2 Shaun Grapes 30 3 Simi Apples 32 4 Luka Mangos 43 5 Mike Apples 45 6 Arun Mangos 35 7 Riti Grapes 37
Now we want to select only those rows in this DataFrame, where column ‘Product’ has value ‘Apples’, like this,
Name Product Sale 0 Mark Apples 44 3 Simi Apples 32 5 Mike Apples 45
Let’s see how to do that. First of all we wll create a DataFrame,
Advertisements

import pandas as pd # List of Tuples students = [('Mark', 'Apples', 44), ('Aadi', 'Mangos', 31), ('Shaun', 'Grapes', 30), ('Simi', 'Apples', 32), ('Luka', 'Mangos', 43), ('Mike', 'Apples', 45), ('Arun', 'Mangos', 35), ('Riti', 'Grapes', 37),] # Create a DataFrame object df = pd.DataFrame( students, columns = ['Name' , 'Product', 'Sale']) # Display the DataFrame print(df)
Output
Name Product Sale 0 Mark Apples 44 1 Aadi Mangos 31 2 Shaun Grapes 30 3 Simi Apples 32 4 Luka Mangos 43 5 Mike Apples 45 6 Arun Mangos 35 7 Riti Grapes 37
Now select the column ‘Product’ from this DataFrame and apply a condition to it i.e.
boolSeries = df['Product'] == 'Apples' # Boolean Series print(boolSeries)
Output
0 True 1 False 2 False 3 True 4 False 5 True 6 False 7 False Name: Product, dtype: bool
It will return a boolean Series, where each True value indicates the value ‘Apples’ at the corresponding index position in the column. So, basically this Series contains True values for the rows where our condition results in True. Now, if we pass this boolean Series to the subscript operator of DataFrame, then it will select only those rows from the DataFrame for which value in the bool Series is True. For example,
# Select only those rows where, # column 'Product' has value 'Apples' df = df[df['Product'] == 'Apples'] # Display the DataFrame print(df)
Output
Name Product Sale 0 Mark Apples 44 3 Simi Apples 32 5 Mike Apples 45
It selected only those rows from the DataFrame where the condition is satisfied i.e. only those rows where column ‘Product’ contains the value ‘Apples’.
Select DataFrame rows based on multiple conditions
Just like in the above solution, we can also apply multiple conditions to filter the contents of the Dataframe. For example, let’s see how to select only those rows from the DataFrame where sales are greater than 30 but less than 40,
# Select only those rows where sale # value is between 30 and 40 df = df[(df['Sale'] > 30) & (df['Sale'] < 40)] # Display the DataFrame print(df)
Output
Name Product Sale 1 Aadi Mangos 31 3 Simi Apples 32 6 Arun Mangos 35 7 Riti Grapes 37
It returned only those rows from DataFrame, where the sale value is between 30 and 40.
How did it work?
- df[‘Sale’] > 30 gave a Boolean Series, which contains the True where values are greater than 30 only
- df[‘Sale’] < 40 gave a Boolean Series, which includes the True where values are less than 40.
Then we applied the boolean & operator on these two boolean Series. It will select True values only at those indices where both the conditions are True. Then we passed that final boolean Series to the [] operator of DataFrame. It returned only those rows from the DataFrame for which value in the final Bool series was True.
Summary
We learned about different ways to select elements from DataFrame based on conditions.
Pandas Tutorials -Learn Data Analysis with Python
Are you looking to make a career in Data Science with Python?
Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.
Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.
Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.
Join a LinkedIn Community of Python Developers
Recommend
-
16
How to add rows in a DataFrame using dataframe.append() & loc[] , iloc[] – thispointer.comIn this article we will discuss how to add a single or multiple rows in a dataframe using dataframe.append() or loc & iloc. Pandas Datafr...
-
12
How to drop rows in Pandas DataFrame by index labels? Last Updated: 02-07-2020 Pandas provide data analysts a way to delete and filter data frame using
-
10
Drop Duplicate Rows from Pandas Dataframe – thisPointer In this article, we will discuss different ways to delete duplicate rows in a pandas DataFrame. Table of Contents: A DataFrame is a data structure...
-
12
Pandas Tutorial Part #13 – Iterate over Rows & Columns of DataFrame This tutorial will discuss how to iterate over rows or columns of a DataFrame by index positions or label names. Table Of Co...
-
5
Pandas Tutorial Part #10 – Add/Remove DataFrame Rows & Columns In this tutorial, we will learn how to add a new row or column to a DataFrame and change the values of existing rows and columns.
-
8
How to Create an Empty DataFrame and Append Rows & Columns to it in Pandas?How to Create an Empty DataFrame and Append Rows & Columns to it in Pandas?Hello everyone. Welcome to Ge...
-
8
How to Drop Rows in Pandas DataFrame by Index LabelsSkip to content
-
9
In this article, we will discuss different ways to create a Pandas DataFrame and append one row at a time in it. Table Of Contents What is a DataFrame? Pandas DataFrame is two dimen...
-
8
Iterate over Rows of DataFrame in Pandas This article will discuss six different techniques to iterate over a dataframe row by row. Then we will also discuss how to update the contents of a Dataframe whil...
-
8
Remove duplicate rows from a pandas dataframe: Case Insenstive comparison ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK