Member-only story
How to Query Pandas Dataframe With the query() Method?

Introduction
If you have been using pandas for data analysis and are still not aware of the Pandas query method, then you are not alone. The query method is intuitive, clean, and less verbose than the traditional methods of filtering rows from DataFrame. But, for some reason, the Pandas query method is still not being used widely despite its simplicity.
So, in this article, we would like to introduce you to the Pandas query method, and syntax with examples so that you can start using it immediately.
You can access the complete code for this article from GitHub here.
Originally published at pythonsimplifed.com on Oct 18, 2022.
Pandas query method
Pandas’ query method (df.query()
) is used to query the DataFrame columns and filter only the required rows from the DataFrame.
The most commonly used methods to filter the rows from the DataFrame are boolean indexing and positional indexing methods. Refer to the examples below of what I mean by boolean indexing and positional indexing. We will use the tips dataset from the Seaborn library for the examples.
import pandas as pd
import seaborn as sns
df = sns.load_dataset('tips')
If we wanted to select only rows with two conditions — non-smoker (smoker=’No’) and Female, then with boolean indexing and positional indexing, we would have written code as below.
# boolean indexing
df[ (df['smoker']=='No') & (df['sex'] =='Female') ]
# positional indexing using .loc[] and .iloc[]
df.loc[(df['smoker'] == 'No') & (df['sex'] == 'Female')]
But, the above code can also be rewritten using the query method as below. As you can see, despite its being intuitive, clean, and less verbose, the query method is often overlooked by pandas users. In the next section, let’s understand the query method in detail.
df.query('smoker == "No" & sex=="Female"')
Syntax
The syntax for the query method is as shown below —
DataFrame.query(expr, inplace=False, **kwargs)
- expr — It is the expression/conditions to filter the…