0
0
Pandasdata~10 mins

str.replace() for substitution in Pandas - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to replace all occurrences of 'cat' with 'dog' in the 'animal' column.

Pandas
df['animal'] = df['animal'].str.[1]('cat', 'dog')
Drag options to blanks, or click blank then click option'
Aswap
Bsubstitute
Creplace
Dchange
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not exist like 'substitute' or 'swap'.
Trying to replace without using the str accessor.
2fill in blank
medium

Complete the code to replace all digits in the 'code' column with '#' using a regular expression.

Pandas
df['code'] = df['code'].str.[1]('\d', '#', regex=True)
Drag options to blanks, or click blank then click option'
Aswap
Bsub
Ctranslate
Dreplace
Attempts:
3 left
💡 Hint
Common Mistakes
Using sub which is not a pandas string method.
Not setting regex=True when using a regex pattern.
3fill in blank
hard

Fix the error in the code to replace 'yes' with 'no' in the 'answer' column.

Pandas
df['answer'] = df['answer'].str.[1]('yes', 'no')
Drag options to blanks, or click blank then click option'
Areplace
Bstr.replace()
Cstr.replace
Dreplace()
Attempts:
3 left
💡 Hint
Common Mistakes
Including parentheses in the method name inside the code string.
Using str.replace without calling it as a method.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their uppercase form only if the word length is greater than 4.

Pandas
{word: word[1]() for word in words if len(word) [2] 4}
Drag options to blanks, or click blank then click option'
A.upper
B>
C<
D.lower
Attempts:
3 left
💡 Hint
Common Mistakes
Using .lower instead of .upper.
Using less than operator instead of greater than.
5fill in blank
hard

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.

Pandas
{ [1]: [2] for word in words if len(word) [3] 6 }
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
C<
Dword.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using word.lower() instead of word.upper().
Using greater than operator instead of less than.