Open In App

Pandas query() Method

Last Updated : 29 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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()

 



Previous Article
Next Article

Similar Reads

Selecting with complex criteria using query method in Pandas
In this article, let's discuss how to select complex criteria using the Query() method in Pandas. In pandas for Selecting with complex criteria using the query method, first, we create data frames with the help of pandas.Dataframe() and store it one variable and then with the help of query() method we can select complex criteria. With the help of p
2 min read
Pandas DataFrame iterrows() Method | Pandas Method
Pandas DataFrame iterrows() iterates over a Pandas DataFrame rows in the form of (index, series) pair. This function iterates over the data frame column, it will return a tuple with the column name and content in the form of a series. Example: Python Code import pandas as pd df = pd.DataFrame({ 'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 32, 3
2 min read
Pandas DataFrame interpolate() Method | Pandas Method
Python is a great language for data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.  Python Pandas interpolate() method is used to fill NaN values in the DataFrame or Series using various interpolation techniques to fill the m
3 min read
Pandas DataFrame duplicated() Method | Pandas Method
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 and makes importing and analyzing data much easier. Pandas duplicated() method identifies duplicated rows in a DataFrame. It returns a boolean series which is True only for unique rows. Ex
3 min read
How to Filter Rows Based on Column Values with query function in Pandas?
In this article, let's see how to filter rows based on column values. Query function can be used to filter rows based on column values. Consider below Dataframe: C/C++ Code import pandas as pd data = [['A', 10], ['B', 15], ['C', 14], ['D', 12]] df = pd.DataFrame(data, columns = ['Name', 'Age']) df Output: [caption width="800"]Our DataFrame [/captio
1 min read
How to Convert SQL Query Results to Pandas Dataframe Using pypyodbc?
In this article, we are going to see how to convert SQL Query results to a Pandas Dataframe using pypyodbc module in Python. We may need database results from the table using different queries to work on the data and apply any machine learning on the data to analyze the things and the suggestions better. We can convert our data into python Pandas d
2 min read
Pandas DataFrame hist() Method | Create Histogram in Pandas
A histogram is a graphical representation of the numerical data. Sometimes you'll want to share data insights with someone, and using graphical representations has become the industry standard. Pandas.DataFrame.hist() function plots the histogram of a given Data frame. It is useful in understanding the distribution of numeric variables. This functi
4 min read
Pandas Series dt.day_name() Method | Get Day From Date in Pandas
Pandas dt.day_name() method returns the day names of the DateTime Series objects with specified locale. Example C/C++ Code import pandas as pd sr = pd.Series(['2012-12-31 08:45', '2019-1-1 12:30', '2008-02-2 10:30', '2010-1-1 09:25', '2019-12-31 00:00']) idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5'] sr.index = idx sr = pd.to_datetime(sr) resu
2 min read
Pandas Series dt.weekofyear Method | Get Week of Year in Pandas Series
The dt.weekofyear attribute returns a Series containing the week ordinal of the year in the underlying data of the given series object. Example C/C++ Code import pandas as pd sr = pd.Series(['2012-10-21 09:30', '2019-7-18 12:30', '2008-02-2 10:30', '2010-4-22 09:25', '2019-11-8 02:22']) idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5'] sr.index =
2 min read
Python | pandas.to_markdown() in Pandas
With the help of pandas.to_markdown() method, we can get the markdown table from the given dataframes by using pandas.to_markdown() method. Syntax : pandas.to_markdown() Return : Return the markdown table. Example #1 : In this example we can see that by using pandas.to_markdown() method, we are able to get the markdown table from the given datafram
1 min read