Recall & Review
beginner
What does
str.replace() do in pandas?str.replace() substitutes parts of strings in a pandas Series or DataFrame column with another string.
Click to reveal answer
beginner
How do you replace all occurrences of 'cat' with 'dog' in a pandas Series named
pets?pets.str.replace('cat', 'dog')Click to reveal answer
intermediate
What parameter do you use in
str.replace() to treat the pattern as a regular expression?The regex parameter. By default, it is True, meaning the pattern is treated as a regex.
Click to reveal answer
intermediate
How can you replace only the first occurrence of a pattern in each string using
str.replace()?Use the n=1 parameter to limit replacement to the first match.
Click to reveal answer
intermediate
What happens if you set
regex=False in str.replace()?The pattern is treated as a literal string, not a regular expression.
Click to reveal answer
What is the default behavior of the
pattern parameter in str.replace()?✗ Incorrect
By default, str.replace() treats the pattern as a regular expression unless regex=False is set.
How do you replace all occurrences of 'apple' with 'orange' in a pandas Series
fruits?✗ Incorrect
str.replace() is used to substitute strings inside pandas Series.
Which parameter limits the number of replacements in
str.replace()?✗ Incorrect
The n parameter controls how many replacements to make.
If you want to replace the literal string '.' (dot), what should you do?
✗ Incorrect
Dot is a special regex character, so escape it or set regex=False to treat it literally.
What type of object does
str.replace() return when used on a pandas Series?✗ Incorrect
It returns a new pandas Series with the replacements applied.
Explain how to use
str.replace() in pandas to substitute text in a Series. Include how to handle regular expressions and limit replacements.Think about how you change words in a list of sentences.
You got /4 concepts.
Describe a real-life example where
str.replace() in pandas can help clean data.Imagine fixing a messy list of names or addresses.
You got /4 concepts.