
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python - Filter Rows Based on Column Values with query function in Pandas?
To filter rows based on column values, we can use the query() function. In the function, set the condition through which you want to filter records. At first, import the required library −
import pandas as pd
Following is our data with Team Records −
Team = [['India', 1, 100],['Australia', 2, 85],['England', 3, 75],['New Zealand', 4 , 65],['South Africa', 5, 50],['Bangladesh', 6, 40]]
Create a DataFrame from above and add columns as well −
dataFrame = pd.DataFrame(Team, columns=['Country', 'Rank', 'Points'])
Use query() to filter records with “Rank” equal to 5 −
dataFrame.query("Rank == 5"))
Example
Following is the complete code −
import pandas as pd # data in the form of list of team rankings Team = [['India', 1, 100],['Australia', 2, 85],['England', 3, 75],['New Zealand', 4 , 65],['South Africa', 5, 50],['Bangladesh', 6, 40]] # Creating a DataFrame and add columns dataFrame = pd.DataFrame(Team, columns=['Country', 'Rank', 'Points']) print"DataFrame...\n",dataFrame # using query to filter rows print"\nFetch Team with Rank 5..\n",dataFrame.query("Rank == 5")
Output
This will produce the following output −
DataFrame... Country Rank Points 0 India 1 100 1 Australia 2 85 2 England 3 75 3 New Zealand 4 65 4 South Africa 5 50 5 Bangladesh 6 40 Fetch Team with Rank 5.. Country Rank Points 4 South Africa 5 50
Example
Let us see another example. Here, we have a different condition to filter rows −
import pandas as pd # data in the form of list of team rankings Team = [['India', 1, 100],['Australia', 2, 85],['England', 3, 75],['New Zealand', 4 , 65],['South Africa', 5, 50],['Bangladesh', 6, 40]] # Creating a DataFrame and add columns dataFrame = pd.DataFrame(Team, columns=['Country', 'Rank', 'Points']) print"DataFrame...\n",dataFrame # using query to filter rows print"\nFetch Team with points above 70 and Rank less than 3..\n" print(dataFrame.query("Points > 70 and Rank <3"))
Output
This will produce the following output −
DataFrame... Country Rank Points 0 India 1 100 1 Australia 2 85 2 England 3 75 3 New Zealand 4 65 4 South Africa 5 50 5 Bangladesh 6 40 Fetch Team with points above 70 and Rank less than 3.. Country Rank Points 0 India 1 100 1 Australia 2 85
- Related Articles
- Select rows from a Pandas DataFrame based on column values
- Filter the rows – Python Pandas
- Python - Filter dictionary key based on the values in selective list
- Deleting a DataFrame row in Python Pandas based on column value
- Creating a Pandas dataframe column based on a given condition in Python
- Python Pandas - Filling missing column values with mode
- Python Pandas - Filling missing column values with median
- Python – Filter rows with required elements
- Python – Filter Rows with Range Elements
- Python - Remove a column with all null values in Pandas
- Python - Filter Pandas DataFrame with numpy
- Replace numerical column values based on character column values in R data frame.
- How to AutoFilter Rows Based on Cell Values in Excel?
- How to filter rows in Pandas by regex?
- How to select rows based on range of values of a column in an R data frame?

Advertisements