df.query() – How does pandas query method works

Spread the love

In our previous post, we talked about how to filter data with loc, iloc and in this post, we will learn to filter data using pandas query method.

Read a dataset –

import pandas as pd
url = "https://raw.githubusercontent.com/bprasad26/lwd/master/data/titanic.csv"
df = pd.read_csv(url)

Less Than –

Let’s say that I want to select all the passengers in titanic whose age is less than 30 years.

df.query('Age < 30')

Greater Than –

All passengers whose age is greater than 30

df.query('Age > 30')

Equal to –

Select all Females passengers

df.query('Sex == "female"')

Not Equal To –

Select all passengers who is Not a Male

df.query('Sex != "male"')

And –

Select all passengers who paid more than $100 and embarked from S.

df.query('(Fare > 100) and (Embarked == "S")')

OR –

Select all passengers who is Female or survived in the accident.

df.query('(Sex == "female") or (Survived == 1)')

IN –

Select all passengers who embarked from either Queenstown or Southampton.

df.query('Embarked in ["Q", "S"]')


Select all passengers who neither embarked from Queenstown or Southampton.

df.query('Embarked not in ["Q", "S"]')

1 . Pandas – Select Rows and Columns from a DataFrame.

Leave a Reply