0
0
Data Analysis Pythondata~10 mins

Pattern matching with str.contains in Data Analysis Python - Interactive Code Practice

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

Complete the code to filter rows where the 'Name' column contains the substring 'John'.

Data Analysis Python
filtered = df[df['Name'].str.[1]('John')]
Drag options to blanks, or click blank then click option'
Acontains
Bstartswith
Cmatch
Dendswith
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'match' which looks for exact matches at the start.
Using 'startswith' or 'endswith' which only check beginnings or ends.
2fill in blank
medium

Complete the code to filter rows where the 'Email' column contains the pattern '@gmail.com' ignoring case.

Data Analysis Python
filtered = df[df['Email'].str.contains('@gmail.com', case=[1])]
Drag options to blanks, or click blank then click option'
A0
BTrue
CNone
DFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using True which makes the search case sensitive.
Using None which is not a valid boolean here.
3fill in blank
hard

Fix the error in the code to filter rows where 'City' contains 'York' using regex.

Data Analysis Python
filtered = df[df['City'].str.contains('York', regex=[1])]
Drag options to blanks, or click blank then click option'
ATrue
B'True'
C1
DFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Passing 'True' as a string causes a TypeError.
Passing 1 instead of True is not accepted.
4fill in blank
hard

Fill both blanks to create a dictionary of word lengths for words longer than 4 characters.

Data Analysis Python
lengths = {word: [1] for word in words if len(word) [2] 4}
Drag options to blanks, or click blank then click option'
Alen(word)
B>
C<
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in the condition.
Using 'word' instead of 'len(word)' for the value.
5fill in blank
hard

Fill all three blanks to create a dictionary of uppercase words with their lengths if length is greater than 3.

Data Analysis Python
result = [1]: [2] for w in words if len(w) [3] 3}
Drag options to blanks, or click blank then click option'
Aw.upper()
Blen(w)
C>
Dw
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'w' instead of 'w.upper()' for keys.
Using '<' instead of '>' in the condition.