Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The str.contains() method checks if the string contains the given pattern.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'match' which checks the whole string.
Using 'startswith' or 'endswith' which check only the start or end.
✗ Incorrect
str.contains() checks if the string contains the specified substring anywhere.
3fill in blank
hardFix 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'
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.
✗ Incorrect
The correct parameter to ignore case is case=False.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Using '<' instead of '>' in the condition.
✗ Incorrect
The dictionary maps each word to its length, filtering words longer than 3 characters.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'count.upper()' which is invalid since count is a number.
Using 'word' as key without uppercasing.
✗ Incorrect
The dictionary keys are uppercase words, values are counts, iterating over word_counts items.