0
0
Pandasdata~10 mins

sort_values() by single column 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 sort the DataFrame by the 'age' column in ascending order.

Pandas
sorted_df = df.sort_values(by=[1])
Drag options to blanks, or click blank then click option'
A'name'
B'height'
C'score'
D'age'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put the column name in quotes.
Using a column name that does not exist in the DataFrame.
2fill in blank
medium

Complete the code to sort the DataFrame by the 'score' column in descending order.

Pandas
sorted_df = df.sort_values(by='score', ascending=[1])
Drag options to blanks, or click blank then click option'
AFalse
BTrue
C'False'
D'True'
Attempts:
3 left
💡 Hint
Common Mistakes
Using strings 'True' or 'False' instead of boolean True or False.
Setting ascending to True which sorts in ascending order.
3fill in blank
hard

Fix the error in the code to correctly sort the DataFrame by the 'height' column.

Pandas
sorted_df = df.sort_values(by=[1])
Drag options to blanks, or click blank then click option'
A'height'
Bheight
Cdf['height']
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the column name without quotes causing a NameError.
Passing the entire column series instead of the column name.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each word to its length, but only for words longer than 4 characters.

Pandas
{word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Blen(word) > 4
Cword > 4
Dword.length()
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself in the condition instead of its length.
Using incorrect syntax like word.length() which is not valid in Python.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each uppercase word to its score, but only if the score is positive.

Pandas
{ [1]: [2] for [3], [2] in data.items() if [2] > 0 }
Drag options to blanks, or click blank then click option'
Ak.upper()
Bv
Ck
Ditem
Attempts:
3 left
💡 Hint
Common Mistakes
Using item instead of unpacking key and value.
Not converting the key to uppercase.
Using the wrong variable names in the comprehension.