Complete the code to replace all occurrences of 'cat' with 'dog' in the 'animal' column.
df['animal'] = df['animal'].str.[1]('cat', 'dog')
str accessor.The str.replace() method replaces all occurrences of a substring with another substring in a pandas Series.
Complete the code to replace all digits in the 'code' column with '#' using a regular expression.
df['code'] = df['code'].str.[1]('\d', '#', regex=True)
sub which is not a pandas string method.regex=True when using a regex pattern.The str.replace() method supports regular expressions when regex=True is set, allowing pattern-based replacements.
Fix the error in the code to replace 'yes' with 'no' in the 'answer' column.
df['answer'] = df['answer'].str.[1]('yes', 'no')
str.replace without calling it as a method.When using pandas string methods, you call str.replace() on the Series with str. accessor, but here the accessor is already used, so just replace is the method name.
Fill both blanks to create a dictionary comprehension that maps words to their uppercase form only if the word length is greater than 4.
{word: word[1]() for word in words if len(word) [2] 4}.lower instead of .upper.The method upper() converts strings to uppercase. The condition len(word) > 4 filters words longer than 4 characters.
Fill all three blanks to create a dictionary comprehension that maps the uppercase word to its length only if the length is less than 6.
{ [1]: [2] for word in words if len(word) [3] 6 }word.lower() instead of word.upper().The key is the uppercase word using word.upper(). The value is the length with len(word). The condition filters words with length less than 6 using <.