Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages that makes importing and analyzing data much easier.
Analyzing data requires a lot of filtering operations. Pandas provide many methods to filter a Data frame and
Dataframe.query() is one of them.
Syntax: DataFrame.query(expr, inplace=False, **kwargs)
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 CSV file used, Click Here.
Dataframe.query() method only works if the column name doesn’t have any empty spaces. So before applying the method, spaces in column names are replaced with ‘_’
Example #1: Single condition filtering
In this example, the data is filtered on the basis of single condition. Before applying the query() method, the spaces in column names have been replaced with ‘_’.
As shown in the output image, the data now only have rows where Senior Management is True.
Example #2: Multiple condition filtering
In this example, dataframe has been filtered on multiple conditions. Before applying the query() method, the spaces in column names have been replaced with ‘_’.
As shown in the output image, only two rows have been returned on the basis of filters applied.
- Data profiling in Pandas using Python
- Data Manipulattion in Python using Pandas
- Python | Data analysis using Pandas
- Python | Pandas Series.data
- Python | Pandas Index.data
- Python | Data Comparison and Selection in Pandas
- Get the data type of column in Pandas - Python
- Python | Bilateral Filtering
- How to Filter and save the data as new files in Excel with Python Pandas?
- Python - Filtering text using Enchant
- Python - Sharpen and blur filtering using pgmagick
- Filtering Images based on size attributes in Python
- Python | Pandas Series.astype() to convert Data type of series
- Pandas.cut() method in Python
- Python | pandas.period_range() method
- Python | pandas.date_range() method
- Python Pandas - get_dummies() method
- Python | pandas.to_numeric method
- Python | Pandas Dataframe.describe() method
- Python | Pandas DataFrame.to_latex() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Akanksha_Rai