Signup/Sign In

Pandas DataFrame query() Method

In this tutorial, we will discuss and learn the Python pandas DataFrame.query() method which is similar to the filter method. This method queries or filters the DataFrame by boolean expression. In this tutorial, we will filter the DataFrame by a single column, by comparing two columns, and by using the 'AND' operator.

The below is the syntax of the DataFrame.query() method.

Syntax

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

Parameters

expr: It represents the str that is the query string to evaluate.

inplace: It represents the bool. It indicates that whether the query should modify the data in place or return a modified copy.

**kwargs: Additional keyword arguments to be passed to the function.

Example: DataFrame.query() Method in Pandas

Let's create a DataFrame and filter the DataFrame by a single column using the less than and greater than expression. See the below example, the the DataFrame.query() method returns the DataFrame which contains the information whose age is above 22 and weight is greater than and equal to 60.

#importing pandas as pd
import pandas as pd
df=pd.DataFrame({'Name':['Chetan','yashas','yuvraj','Pooja','Sindu','Renuka'],'Age':[19,26,22,24,21,23],'Height':[165,150,168,157,155,170],'Weight':[60,65,70,50,52,55]})
print("------printing the info whose age is above 22-------")
print(df.query('Age<23'))
print("------printing the info whose weight is above 60-----")
print(df.query('Weight>=60'))


------printing the info whose age is above 22-------
Name Age Height Weight
0 Chetan 19 165 60
2 yuvraj 22 168 70
4 Sindu 21 155 52
------printing the info whose weight is above 60-----
Name Age Height Weight
0 Chetan 19 165 60
1 yashas 26 150 65
2 yuvraj 22 168 70

Example: Filtering the DataFrame by using DataFrame.query() Method

Let's create a DataFrame and filter the DataFrame by comparing two columns using the DataFrame.query() method. See the below example, the the DataFrame.query() method returns the DataFrame if the values in the sci_Marks column are greater than the Maths_Marks column values.

#importing pandas as pd
import pandas as pd
df=pd.DataFrame({'Name':['Chetan','yashas','yuvraj','Pooja','Sindu','Renuka'],'sci_Marks':[85,70,75,90,95,70],'Maths_Marks':[82,79,80,89,92,70]})
print("------comparing sci_Marks and Maths_Marks columns -------")
print(df.query('sci_Marks > Maths_Marks'))


------comparing sci_Marks and Maths_Marks columns -------
Name sci_Marks Maths_Marks
0 Chetan 85 82
3 Pooja 90 89
4 Sindu 95 92

Example 3: Filtering the DataFrame by using DataFrame.query() Method

Let's create a DataFrame and filter the DataFrame columns by AND operator. The DataFrame.query() method returns the DataFrame if both the operands are True and the returned DataFrame consists of values that the condition matches.

#importing pandas as pd
import pandas as pd
df=pd.DataFrame({'Name':['Chetan','yashas','yuvraj','Pooja','Sindu','Renuka'],'Age':[19,26,22,24,21,23],'Height':[165,150,168,157,155,170],'Weight':[60,65,70,50,52,55]})
print("------printing the info whose Height is above 155 and weight is above 60-------")
print(df.query('Height > 155' and 'Weight > 60'))


------printing the info whose Height is above 155 and weight is above 60-------
Name Age Height Weight
1 yashas 26 150 65
2 yuvraj 22 168 70

Example 4: Filtering the DataFrame by using DataFrame.query() Method

If the parameter inplace=True, it just modifies the DataFrame by the given condition. It does not return a new DataFrame, instead, it returns None. See the below example. We can check the modified DataFrame by checking the DataFrame.

#importing pandas as pd
import pandas as pd
df=pd.DataFrame({'Name':['Chetan','yashas','yuvraj','Pooja','Sindu','Renuka'],'Age':[19,26,22,24,21,23],'Height':[165,150,168,157,155,170],'Weight':[60,65,70,50,52,55]})
print(df.query('Age==24',inplace=True))
print("-----The modified DataFrame is-----")
print(df)


None
-----The modified DataFrame is-----
Name Age Height Weight
3 Pooja 24 157 50

Conclusion

In this tutorial, we learned the Python pandas DataFrame.query() method. We learned the syntax, parameters and applied this method on the DataFrame to understand the DataFrame.query() method.



About the author:
I like writing about Python, and frameworks like Pandas, Numpy, Scikit, etc. I am still learning Python. I like sharing what I learn with others through my content.