Q#88: Filtering strings in Pandas dataframes

Panda with a string

Given the following dataframe, write code using Python (Pandas) to return the rows that contain the string ‘J’ in the name column (to practice searching string contains).

Next, write code to return all rows where favorite_color does not contain the string ‘r’.

TRY IT YOURSELF

ANSWER

An easy question for you today, just testing your ability to filter in the pandas library.

Task 1: Finding Rows with a Specific Character in 'Name' Column

Let's start by loading the DataFrame and returning the rows that contain the character 'J' in the 'Name' column:

import pandas as pd

# Sample DataFrame
data = {
'Age': [20, 19, 22, 21],
'Favorite Color': ['blue', 'blue', 'yellow', 'green'],
'Grade': [88, 95, 92, 70],
'Name': ['Willard Morris', 'Al Jennings', 'Omar Mullins', 'Spencer McDaniel']
}

df = pd.DataFrame(data)

# Filter rows containing 'J' in the 'Name' column
result_df = df[df['Name'].str.contains('J')]

print(result_df)

Explanation

In the code above, we used Pandas' str.contains() method to search for rows containing the character 'J' in the 'Name' column. The method returns a boolean Series indicating whether the condition is met for each row. By passing this boolean Series as an index to the DataFrame, we filtered and obtained the desired rows.

Task 2: Finding Rows Where 'Favorite Color' Does Not Contain 'r'

Next, let's find the rows where the 'Favorite Color' column does not contain the substring 'r':

# Filter rows where 'Favorite Color' does not contain 'r'
result_df = df[~df['Favorite Color'].str.contains('r', case=False)]

print(result_df)

Explanation

In the above code, we used the tilde (~) operator to negate the boolean Series obtained by applying the str.contains() method on the 'Favorite Color' column. This way, we get rows where the condition is False, meaning the substring 'r' is not present in the 'Favorite Color' column.

Plug: Checkout all my digital products on Gumroad here. Please purchase ONLY if you have the means to do so. Use code: MEDSUB to get a 10% discount!

Tips and Donations

--

--