How to use dataframe.query() method In Python Pandas
The query() method takes up the expression that returns a boolean value, processes all the rows in the Dataframe, and returns the resultant Dataframe with selected rows.
Example 1: Pandas select rows by Dataframe.query() method based on column values
Select rows where the name=”Albert”
Python3
df.query( 'name=="Albert"' ) |
Output
Example 2: Select rows based on iple column conditions
This example is to demonstrate that logical operators like AND/OR can be used to check multiple conditions. we are trying to select rows where points>50 and the player is not Albert.
Python3
df.query( 'points>50 & name!="Albert"' ) |
Output
How to select rows from a dataframe based on column values ?
Prerequisite: Pandas.Dataframes in Python
In this article, we will cover how we select rows from a DataFrame based on column values in Python.
The rows of a Dataframe can be selected based on conditions as we do use the SQL queries. The various methods to achieve this is explained in this article with examples.