Search pandas column with string contains and does not contain


import pandas as pd
import numpy as np

Create a dataframe

#create a dataframe
raw_data = {'name': ['Willard Morris', 'Al Jennings'],
'age': [20, 19],
'favorite_color': ['blue', 'red'],
'grade': [88, 92],
'grade': [88, 92]}
df = pd.DataFrame(raw_data, index = ['Willard Morris', 'Al Jennings'])
df
age favorite_color grade name
Willard Morris 20 blue 88 Willard Morris
Al Jennings 19 red 92 Al Jennings


Search pandas column with string contains

#here we can count the number of distinct users viewing on a given day
new_df = df[df['name'].str.contains('Morris', na=False)]
new_df.head()
age favorite_color grade name
Willard Morris 20 blue 88 Willard Morris


Search pandas column with string does not contain

#here we can count the number of distinct users viewing on a given day
new_df2 = df[~df['name'].str.contains('Morris', na=False)]
new_df2.head()
age favorite_color grade name
Al Jennings 19 red 92 Al Jennings


Ace your next data science interview

Get better at data science interviews by solving a few questions per week



Find a bug? Submit a suggested change on Github, or message me on Twitter.