Analyzing data requires a lot of filtering operations. Pandas provide many methods for filtering a data frame, and Dataframe.query()
is one of them.
Syntax: DataFrame.query(expr, inplace = False, ** kwargs)
Parameters:
expr: Expression in string form to filter data.
inplace: Make changes in the original data frame if True
kwargs: Other keyword arguments.Return type: Filtered Data frame
To download the used CSV file, click here.
Note. Dataframe.query ()
only works if the column name no spaces. Therefore, before applying the method, spaces in column names are replaced with "_"
Example # 1: Filtering one condition
This example filters data based on one condition ... Before using the query () method, spaces in column names were replaced with & # 39; _ & # 39 ;.
| tr>
Output:
As shown in the output image, there are now only those in the data lines where senior control is True.
Example # 2: filtering multiple conditions
In this example, the dataframe has been filtered by several conditions. Before using the query () method, spaces in column names were replaced with & # 39; _ & # 39 ;.
|
Output:
As shown in the output image, only two rows were returned based on the filters applied.