Select rows that contain specific text using Pandas

07 Apr, 2021
While preprocessing data using pandas dataframe there may be a need to find the rows that contain specific text. In this article we will discuss methods to find the rows that contain specific text in the columns or rows of a dataframe in pandas.
Dataset in use:
Using the contains() function of strings to filter the rows. We are filtering the rows based on the ‘Credit-Rating’ column of the dataframe by converting it to string followed by the contains method of string class. contains() method takes an argument and finds the pattern in the objects that calls it.
Example:
Python3
# importing pandas as pdimport pandas as pd  # reading csv filedf = pd.read_csv("Assignment.csv")  # filtering the rows where Credit-Rating is Fairdf = df[df['Credit-Rating'].str.contains('Fair')]print(df)
Output : 
Rows containing Fair as Savings
Using itertuples() to iterate rows with find to get rows that contain the desired text. itertuple method return an iterator producing a named tuple for each row in the DataFrame. It works faster than the iterrows() method of pandas.
Example:
Python3
# importing pandas as pdimport pandas as pd  # reading csv filedf = pd.read_csv("Assignment.csv")  # filtering the rows where Age_Range contains Youngfor x in df.itertuples():    if x[2].find('Young') != -1:        print(x)
Output : 
Rows with Age_Range as Young
Using iterrows() to iterate rows with find to get rows that contain the desired text. iterrows() function returns the iterator yielding each index value along with a series containing the data in each row. It is slower as compared to the itertuples because of lot of type checking done by it.
Example:
Python3
# importing pandas as pdimport pandas as pd  # reading csv filedf = pd.read_csv("Assignment.csv")  # filtering the rows where job is Govtfor index, row in df.iterrows():    if 'Govt' in row['job']:        print(index, row['job'], row['Age_Range'],              row['Salary'], row['Savings'], row['Credit-Rating'])
Output : 
Rows with job as Govt
Using regular expressions to find the rows with the desired text. search() is a method of the module re. re.search(pattern, string): It is similar to re.match() but it doesn’t limit us to find matches at the beginning of the string only. We are iterating over the every row and comparing the job at every index with ‘Govt’ to only select those rows.
Example:
Python3
# using regular expressionsfrom re import search  # import pandas as pdimport pandas as pd  # reading CSV filedf = pd.read_csv("Assignment.csv")  # iterating over rows with job as Govt and printingfor ind in df.index:    if search('Govt', df['job'][ind]):        print(df['job'][ind], df['Savings'][ind],              df['Age_Range'][ind], df['Credit-Rating'][ind])
Output : 
Rows where job is Govt
Picked
Python Pandas-exercise
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Iterate over a list in Python
Python Classes and Objects
Convert integer to string in Python