0
0
Pandasdata~5 mins

str.contains() for pattern matching in Pandas - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the str.contains() method do in pandas?

The str.contains() method checks if each string in a pandas Series contains a specified pattern or substring. It returns a Series of True or False values.

Click to reveal answer
beginner
How can you use str.contains() to find rows with a specific word in a DataFrame column?

You can use df['column'].str.contains('word') to get a boolean Series. Then use it to filter rows: df[df['column'].str.contains('word')].

Click to reveal answer
beginner
What parameter do you use in str.contains() to ignore case when matching?

Use the parameter case=False to make the pattern matching ignore uppercase or lowercase differences.

Click to reveal answer
intermediate
What happens if the data contains missing values (NaN) when using str.contains()?

By default, str.contains() returns NaN for missing values. You can use na=False to treat missing values as False.

Click to reveal answer
intermediate
Can str.contains() use regular expressions for pattern matching?

Yes, by default str.contains() uses regular expressions. You can disable this by setting regex=False if you want to match exact substrings.

Click to reveal answer
What does df['col'].str.contains('abc') return?
AThe count of 'abc' in the column
BA filtered DataFrame with rows containing 'abc'
CA Series of True/False indicating if 'abc' is in each string
DThe first string containing 'abc'
How do you make str.contains() ignore case?
AUse <code>case=True</code>
BUse <code>case=False</code>
CUse <code>ignore_case=True</code>
DUse <code>ignore=True</code>
What parameter do you use to treat missing values as False in str.contains()?
Ana=False
Bna=True
Cfillna=False
Dmissing=False
If you want to match the exact substring without regex, what do you do?
ASet <code>regex=False</code>
BSet <code>regex=True</code>
CUse <code>exact=True</code>
DUse <code>match=True</code>
What type of object does str.contains() return?
ADataFrame
BString
CList
DBoolean Series
Explain how to use str.contains() to filter rows in a DataFrame based on a pattern.
Think about how to get True/False for each row and then select rows.
You got /4 concepts.
    Describe the role of the regex, case, and na parameters in str.contains().
    These parameters customize how the pattern matching works.
    You got /3 concepts.