It checks if each string in a pandas Series contains a specified pattern or substring, returning a Series of True or False values.
Click to reveal answer
beginner
How can you make str.contains() case insensitive?
By setting the parameter case=False, the method ignores case when searching for the pattern.
Click to reveal answer
intermediate
What happens if str.contains() encounters a missing value (NaN) in the Series?
By default, it returns NaN for that position, but you can set na=False to treat missing values as not matching the pattern.
Click to reveal answer
beginner
How do you use str.contains() to filter rows in a DataFrame where a column contains a specific word?
Use it inside boolean indexing like df[df['column'].str.contains('word', na=False)] to keep rows where 'word' appears in 'column'.
Click to reveal answer
intermediate
Can str.contains() use regular expressions for pattern matching?
Yes, by default it treats the pattern as a regular expression, allowing complex pattern matching. You can disable this with regex=False.
Click to reveal answer
What does str.contains('cat') return when applied to a pandas Series?
AA Series of True/False indicating if 'cat' is in each string
BA list of strings containing 'cat'
CThe count of 'cat' occurrences
DThe original Series unchanged
✗ Incorrect
It returns a Series of True or False values showing if 'cat' is found in each string.
How do you ignore case when using str.contains()?
ASet <code>case=False</code>
BSet <code>ignore_case=True</code>
CSet <code>case=True</code>
DUse <code>lower()</code> on the Series first
✗ Incorrect
Setting case=False makes the search case insensitive.
What parameter do you use to treat NaN values as False in str.contains()?
Aignore_na=True
Bna=True
Cfillna=False
Dna=False
✗ Incorrect
The na=False parameter treats missing values as not matching the pattern.
By default, does str.contains() treat the pattern as a regular expression?
AOnly if the pattern is a string
BNo
CYes
DOnly if <code>regex=True</code> is set
✗ Incorrect
By default, str.contains() uses regex for pattern matching.
Which of these filters a DataFrame to rows where 'name' column contains 'John'?
Adf[df['name'].contains('John')]
Bdf[df['name'].str.contains('John', na=False)]
Cdf[df['name'].str.match('John')]
Ddf[df['name'].str.find('John')]
✗ Incorrect
Use str.contains() with na=False inside boolean indexing to filter rows.
Explain how to use str.contains() to find rows in a DataFrame where a column includes a certain substring, including how to handle missing values and case sensitivity.
Think about filtering with True/False and controlling how missing data and letter case affect the search.
You got /4 concepts.
Describe the role of regular expressions in str.contains() and how to disable them if you want a simple substring search.
Consider how regex allows patterns and how to turn it off for plain text matching.
You got /4 concepts.
Practice
(1/5)
1. What does the str.contains method do when used on a pandas Series?
easy
A. It replaces all occurrences of a pattern with another string.
B. It counts how many times a pattern appears in each string.
C. It splits each string into a list based on a pattern.
D. It checks if each string contains a specified pattern and returns true or false.
Solution
Step 1: Understand str.contains purpose
The method checks each string in a Series for a pattern and returns true or false accordingly.
Step 2: Compare with other string methods
Replacing, splitting, or counting are different methods like replace(), split(), or count(), not contains().
Final Answer:
It checks if each string contains a specified pattern and returns true or false. -> Option D
Quick Check:
str.contains returns boolean mask [OK]
Hint: Remember: contains returns true/false for pattern presence [OK]
Common Mistakes:
Confusing contains with replace or split
Thinking it returns counts instead of booleans
Assuming it modifies the original data
2. Which of the following is the correct syntax to filter rows in a DataFrame df where the column 'name' contains the substring 'cat'?
easy
A. df[df['name'].str.contain('cat')]
B. df[df['name'].contains('cat')]
C. df[df['name'].str.contains('cat')]
D. df[df['name'].str.contains(cat)]
Solution
Step 1: Identify correct method usage
The correct method is str.contains called on the Series with the pattern as a string argument.
Step 2: Check syntax details
df[df['name'].str.contains('cat')] uses str.contains('cat') correctly with quotes around 'cat'. df[df['name'].contains('cat')] misses str, C has a typo, D misses quotes around 'cat'.
Final Answer:
df[df['name'].str.contains('cat')] -> Option C
Quick Check:
Use str.contains with string pattern in quotes [OK]
Hint: Use df['col'].str.contains('pattern') with quotes [OK]
Common Mistakes:
Forgetting to use .str before contains
Missing quotes around the pattern string
Typo in method name like contain instead of contains
A. TypeError because df is not defined; import pandas first.
B. NameError because 'cat' is not quoted; fix by using 'cat' in quotes.
C. AttributeError because str.contains is misspelled; fix spelling.
D. No error; code runs correctly.
Solution
Step 1: Identify cause of error
The pattern 'cat' is used without quotes, so Python treats it as a variable, causing NameError.
Step 2: Correct the syntax
Put 'cat' in quotes: str.contains('cat') to pass a string pattern.
Final Answer:
NameError because 'cat' is not quoted; fix by using 'cat' in quotes. -> Option B
Quick Check:
Pattern must be a string in quotes [OK]
Hint: Always quote string patterns in str.contains [OK]
Common Mistakes:
Forgetting quotes around pattern
Assuming pattern can be variable without definition
Ignoring error messages about undefined names
5. You have a DataFrame df with a column 'comments' containing text. You want to filter rows where 'comments' contain either 'error' or 'fail' (case insensitive). Which code correctly does this?
hard
A. df[df['comments'].str.contains('error|fail', case=false)]
B. df[df['comments'].str.contains('error' or 'fail', case=true)]
C. df[df['comments'].str.contains(['error', 'fail'], case=false)]
D. df[df['comments'].str.contains('error|fail', case=true)]
Solution
Step 1: Use regex pattern for multiple words
To match 'error' or 'fail', use regex 'error|fail' inside str.contains.
Step 2: Set case insensitive matching
Use case=false to ignore case differences.
Step 3: Check options
df[df['comments'].str.contains('error|fail', case=false)] correctly uses regex and case=false. df[df['comments'].str.contains('error' or 'fail', case=true)] misuses 'or' inside string, C passes list instead of string, A uses case=true which is case sensitive.
Final Answer:
df[df['comments'].str.contains('error|fail', case=false)] -> Option A
Quick Check:
Use regex with | and case=false for case-insensitive OR [OK]
Hint: Use regex 'word1|word2' with case=false for OR matching [OK]
Common Mistakes:
Using Python 'or' inside string instead of regex |
Passing list instead of string pattern
Forgetting case=false for case-insensitive search
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.