Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to sort the DataFrame df by the column 'age' in ascending order.
Pandas
sorted_df = df.sort_values(by=[1]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a column name that does not exist in the DataFrame.
Not passing the column name as a string.
✗ Incorrect
The sort_values function sorts the DataFrame by the column specified in the 'by' parameter. Here, we want to sort by 'age'.
2fill in blank
mediumComplete the code to sort the DataFrame df by the column 'score' in descending order.
Pandas
sorted_df = df.sort_values(by='score', ascending=[1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the boolean value as a string instead of a boolean.
Setting ascending to True which sorts in ascending order.
✗ Incorrect
Setting ascending=False sorts the DataFrame in descending order.
3fill in blank
hardFix the error in the code to sort the DataFrame df by the column 'height'.
Pandas
sorted_df = df.sort_values(by=[1]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the column name without quotes causing a NameError.
Using a list instead of a string when only one column is needed.
✗ Incorrect
The 'by' parameter requires the column name as a string, so it must be enclosed in quotes.
4fill in blank
hardFill both blanks to create a dictionary comprehension that maps each word to its length only if the length is greater than 3.
Pandas
lengths = {word: [1] for word in words if [2] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself instead of its length as the value.
Using the word in the condition instead of its length.
✗ Incorrect
The dictionary comprehension maps each word to its length using len(word). The condition filters words with length greater than 3 using len(word) > 3.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps each uppercase word to its score only if the score is greater than 50.
Pandas
result = { [1]: [2] for word, score in data.items() if [3] } Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using word.lower() instead of word.upper().
Not filtering scores correctly.
✗ Incorrect
The comprehension maps the uppercase version of each word to its score. The condition filters scores greater than 50.