

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to filter rows in Pandas by regex?
A regular expression (regex) is a sequence of characters that define a search pattern. To filter rows in Pandas by regex, we can use the str.match() method.
Steps
- Create a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.
- Print the input DataFrame, df.
- Initialize a variable regex for the expression. Supply a string value as regex, for example, the string 'J.*' will filter all the entries that start with the letter 'J'.
- Use df.column_name.str.match(regex) to filter all the entries in the given column name by the supplied regex.
Example
import pandas as pd df = pd.DataFrame( dict( name=['John', 'Jacob', 'Tom', 'Tim', 'Ally'], marks=[89, 23, 100, 56, 90], subjects=["Math", "Physics", "Chemistry", "Biology", "English"] ) ) print "Input DataFrame is:\n", df regex = 'J.*' print "After applying ", regex, " DataFrame is:\n", df[df.name.str.match(regex)] regex = 'A.*' print "After applying ", regex, " DataFrame is:\n", df[df.name.str.match(regex)]
Output
Input DataFrame is: name marks subjects 0 John 89 Math 1 Jacob 23 Physics 2 Tom 100 Chemistry 3 Tim 56 Biology 4 Ally 90 English After applying J.* DataFrame is: name marks subjects 0 John 89 Math 1 Jacob 23 Physics After applying A.* DataFrame is: name marks subjects 4 Ally 90 English
- Related Questions & Answers
- How to find and filter Duplicate rows in Pandas ?
- Filter the rows – Python Pandas
- How to retrieve rows of a series object by regular expression in the pandas filter method?
- How to use regular expressions (Regex) to filter valid emails in a Pandas series?
- Python - Filter Pandas DataFrame by Time
- How to get the rows by using pandas series.first() method?
- Python - Filter Rows Based on Column Values with query function in Pandas?
- Python Pandas - How to select rows from a DataFrame by integer location
- Python – Filter Sorted Rows
- How to filter rows by excluding a particular value in columns of the R data frame?
- Python Pandas - How to select rows from a DataFrame by passing row label
- How to filter rows that contain a certain string in R?
- How to drop duplicate rows in pandas series?
- How is it possible to filter out the duplications in the rows of result set return by MySQL?
- How to filter String list by starting value in Java?
Advertisements