What is the DataFrame.query() method in pandas?
Introduction
The DataFrame.query() method is used to query the features or columns of a DataFrame as a boolean expression. Mostly, it takes an expression as an argument and returns data based on a given boolean condition.
Syntax
DataFrame.query(expr, inplace=False, **kwargs)
Parameters
This method takes the following argument values:
expr: This is a query string that's used to filter results.inplace: The default value of this parameter isFalse. Theinplaceparameter defines whether the query results should change the data in the main DataFrame or not.**kwargs: These are all the additional keyword argument values.
Return value
The DataFrame.query() method either returns filtered data as a pandas DataFrame or nothing if there is no match for the given boolean condition.
Example 1
In this code snippet, we load a data.csv file as a pandas DataFrame and invoke the query() method to fetch all the filtered results as an argument expression.
import pandas as pd# load data.csv in programdf = pd.read_csv("data.csv")# return such employees whose salary is less than their bonusprint(df.query("Salary < Bonus"))
- Line 3:
pd.read_csv("data.csv")loads thedata.csvfile as apandasDataFrame.
Learn more about pd.read_csv().
- Line 5:
df.query("Salary < Bonus")filters data through a DataFrame where the salary of an employee is less than their bonus. It returns queried observations as a new DataFrame.
Example 2
import pandas as pd# load data.csv in programdf = pd.read_csv("data.csv")# replacing blank spaces with '-'df.columns = [column.replace(" ", "-") for column in df.columns]# Filter such employees whose# company is Google and salary is less than bonusprint(df.query('Company == "Google" and Salary < Bonus'))
- Line 3:
pd.read_csv("data.csv")loads thedata.csvfile as apandasDataFrame. - Line 8: We filter results for employees who work at Google and whose salary is less than the decided bonus.
The data.csv file
The data.csv file contains all the records of employees from multiple companies. It holds information like their names, designations, salaries, bonuses, total salaries, and the names of their employers.