0
0
Pandasdata~10 mins

str.contains() for pattern matching 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 filter rows where the 'Name' column contains the substring 'John'.

Pandas
filtered = df[df['Name'].str.[1]('John')]
Drag options to blanks, or click blank then click option'
Asearch
Bmatch
Cfind
Dcontains
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'match' instead of 'contains' which looks for exact matches.
Using 'find' or 'search' which are not pandas string methods.
2fill in blank
medium

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

Pandas
gmail_users = df[df['Email'].str.[1]('@gmail.com')]
Drag options to blanks, or click blank then click option'
Acontains
Bendswith
Cstartswith
Dmatch
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'match' which checks the whole string.
Using 'startswith' or 'endswith' which check only the start or end.
3fill in blank
hard

Fix the error in the code to filter rows where 'City' contains 'York' ignoring case.

Pandas
nyc = df[df['City'].str.contains('York', [1]=False)]
Drag options to blanks, or click blank then click option'
Acase_sensitive
Bignore_case
Ccase
Dcase=False
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'case_sensitive' or 'ignore_case' which are not valid parameters.
Passing True instead of False to the 'case' parameter.
4fill in blank
hard

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

Pandas
{word: [1] for word in words if len(word) [2] 3}
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Using '<' instead of '>' in the condition.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their counts only if the count is greater than 1.

Pandas
result = [1]: [2] for [3], count in word_counts.items() if count > 1}}
Drag options to blanks, or click blank then click option'
Aword.upper()
Bcount
Cword
Dcount.upper()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'count.upper()' which is invalid since count is a number.
Using 'word' as key without uppercasing.