Pandas Query for SQL-like Querying

Pandas provide a query() method that enables users to analyze and filter the data just like where clause in SQL. DataFrame.query() method offers a simple way of making the selection and also capable of simplifying the task of index-based selection .

Lets crate a DataFrame..

import pandas as pd import numpy as np df = pd.DataFrame() df['Name'] = ['John', 'Doe', 'Bill','Jim','Harry','Ben'] df['Age'] = [14, 12, 14,11,12,14] df['Category'] = ['A', 'E', 'B','E','C','D'] df['Height'] = [145, 152,167,136,149,161] df['Weight'] = [34, 54,38,39,44,51] df
Name Age Category Height Weight 0 John 14 A 145 34 1 Doe 12 E 152 54 2 Bill 14 B 167 38 3 Jim 11 E 136 39 4 Harry 12 C 149 44 5 Ben 14 D 161 51

Filtering with DataFrame.query()

df.query('Age == 12')
Name Age Category Height Weight 1 Doe 12 E 152 54 4 Harry 12 C 149 44

Multiple condition with DataFrame.query()

df.query('Age >= 11 & Age<=14')
Name Age Category Height Weight 0 John 14 A 145 34 1 Doe 12 E 152 54 2 Bill 14 B 167 38 3 Jim 11 E 136 39 4 Harry 12 C 149 44 5 Ben 14 D 161 51

Select specific columns with DataFrame.query()

df.query('Age == 14') [['Name','Height','Weight']]
Name Height Weight 0 John 145 34 2 Bill 167 38 5 Ben 161 51