The Wayback Machine - https://web.archive.org/web/20250111041648/https://www.geeksforgeeks.org/pandas-query-method/
Open In App

Pandas query() Method

Last Updated : 29 Mar, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages that makes importing and analyzing data much easier. Analyzing data requires a lot of filtering operations. Pandas Dataframe provide many methods to filter a Data frame and Dataframe.query() is one of them.

Pandas query() method Syntax

Syntax: DataFrame.query(expr, inplace=False, **kwargs)

Parameters:

  • expr: Expression in string form to filter data.
  • inplace: Make changes in the original data frame if True
  • kwargs: Other keyword arguments.

Return type: Filtered Data frame

Pandas DataFrame query() Method

Dataframe.query() method only works if the column name doesn’t have any empty spaces. So before applying the method, spaces in column names are replaced with ‘_’ . To download the CSV file used, Click Here. 

Pandas DataFrame query() Examples

Example 1: Single condition filtering In this example, the data is filtered on the basis of a single condition. Before applying the query() method, the spaces in column names have been replaced with ‘_’. 

Python3




# importing pandas package
import pandas as pd
  
# making data frame from csv file
data = pd.read_csv("employees.csv")
  
# replacing blank spaces with '_'
data.columns = 
    [column.replace(" ", "_") for column in data.columns]
  
# filtering with query method
data.query('Senior_Management == True', 
                           inplace=True)
  
# display
data


Output: 

As shown in the output image, the data now only have rows where Senior Management is True. 

Pandas DataFrame query()

 

Example 2: Multiple conditions filtering In this example, Dataframe has been filtered on multiple conditions. Before applying the query() method, the spaces in column names have been replaced with ‘_’. 

Python3




# importing pandas package
import pandas as pd
  
# making data frame from csv file
data = pd.read_csv("employees.csv")
  
# replacing blank spaces with '_'
data.columns = 
    [column.replace(" ", "_") for column in data.columns]
  
# filtering with query method
data.query('Senior_Management == True
           and Gender == "Male" and Team == "Marketing"
           and First_Name == "Johnny"', inplace=True)
  
# display
data


Output: 

As shown in the output image, only two rows have been returned on the basis of filters applied. 

Pandas DataFrame query()

 


Enhance your coding skills with DSA Python, a comprehensive course focused on Data Structures and Algorithms using Python. Over 90 days, you'll explore essential algorithms, learn how to solve complex problems, and sharpen your Python programming skills. This course is perfect for anyone looking to level up their coding abilities and get ready for top tech interviews.
Join the Three 90 Challenge and commit to completing 90% of the course in 90 days to earn a 90% refund. It’s the perfect way to stay focused, track your progress, and reap the rewards of your hard work. Take the challenge and become a DSA expert in Python today


Next Article

Similar Reads

three90RightbarBannerImg