Rows sorted by score from highest to lowest -> Option A
Quick Check:
ascending=False sorts descending [OK]
Hint: False sorts descending, highest first [OK]
Common Mistakes:
Thinking ascending=False sorts ascending
Confusing sorting by name vs score
Expecting an error from ascending=False
4. What is wrong with this code snippet?
df.sort_values(by='height', ascending='False')
medium
A. sort_values does not accept ascending parameter
B. The column name must be a list, not a string
C. No error, code is correct
D. ascending should be a boolean, not a string
Solution
Step 1: Check the type of ascending parameter
The ascending parameter expects a boolean value: True or False, not a string.
Step 2: Identify the error in the code
Here, ascending='False' is a string, which will cause unexpected behavior or error.
Final Answer:
ascending should be a boolean, not a string -> Option D
Quick Check:
ascending must be True/False boolean [OK]
Hint: Use True/False without quotes for ascending [OK]
Common Mistakes:
Passing 'False' as string instead of boolean
Thinking column name must be list always
Assuming no error with wrong ascending type
5. You have a DataFrame df with columns 'city' and 'temperature'. You want to sort it first by 'city' alphabetically ascending, then by 'temperature' descending. Which code does this correctly?
hard
A. df.sort_values(by=['city', 'temperature'], ascending=False)
B. df.sort_values(by=['city', 'temperature'], ascending=[True, False])
C. df.sort_values(by=['temperature', 'city'], ascending=[False, True])
D. df.sort_values(by=['city', 'temperature'], ascending=[False, True])
Solution
Step 1: Understand multi-column sorting
To sort by multiple columns, pass a list to by and a list of booleans to ascending matching each column.
Step 2: Match sorting order to columns
We want 'city' ascending (True) and 'temperature' descending (False), so ascending=[True, False].
Final Answer:
df.sort_values(by=['city', 'temperature'], ascending=[True, False]) -> Option B
Quick Check:
Multi-column sort uses list of booleans [OK]
Hint: Use ascending list matching columns for multi-sort [OK]
Common Mistakes:
Using single boolean for multi-column ascending
Swapping column order in by list
Mixing ascending True/False order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.