
- 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
Filter the rows – Python Pandas
To filter the rows and fetch specific column value, use the Pandas contains() method. At first, let us import the required library with alias −
import pandas as pd
Read the CSV file using the read_csv(). Our CSV file is on the Desktop −
dataFrame = pd.read_csv("C:\Users\amit_\Desktop\CarRecords.csv")
Now, let us filter the rows with specific text −
dataFrame = dataFrame[dataFrame['Car'].str.contains('Lamborghini')]
Example
Following is the code
import pandas as pd # reading csv file dataFrame = pd.read_csv("C:\Users\amit_\Desktop\CarRecords.csv") print("DataFrame...\n",dataFrame) # select rows containing text "Lamborghini" dataFrame = dataFrame[dataFrame['Car'].str.contains('Lamborghini')] print("\nFetching rows with text Lamborghini ...\n",dataFrame)
Output
This will produce the following output −
DataFrame... Car Place UnitsSold 0 Audi Bangalore 80 1 Porsche Mumbai 110 2 RollsRoyce Pune 100 3 BMW Delhi 95 4 Mercedes Hyderabad 80 5 Lamborghini Chandigarh 80 6 Audi Mumbai 100 7 Mercedes Pune 120 8 Lamborghini Delhi 100 Fetching rows with text Lamborghini ... Car Place UnitsSold 5 Lamborghini Chandigarh 80 8 Lamborghini Delhi 100
- Related Articles
- Python – Filter Sorted Rows
- Python - Filter Rows Based on Column Values with query function in Pandas?
- How to filter rows in Pandas by regex?
- How to find and filter Duplicate rows in Pandas ?
- Python – Filter rows with required elements
- Python – Filter Rows with Range Elements
- Python – Filter rows without Space Strings
- Python - Filter Pandas DataFrame with numpy
- Python - Filter Pandas DataFrame by Time
- Python Pandas – Filter DataFrame between two dates
- Python – Filter immutable rows representing Dictionary Keys from Matrix
- Python – Filter rows with Elements as Multiple of K
- Python - Ranking Rows of Pandas DataFrame
- How to retrieve rows of a series object by regular expression in the pandas filter method?
- Python Program to Filter Rows with a specific Pair Sum

Advertisements