Hint: Use by=columns and ascending=list of booleans [OK]
Common Mistakes:
Using wrong parameter names like columns or sort
Passing a single boolean for multiple columns
Mixing ascending order values
3.
Given the DataFrame:
import pandas as pd
df = pd.DataFrame({
'Name': ['Anna', 'Bob', 'Anna', 'Bob'],
'Score': [90, 92, 85, 80]
})
What is the result of df.sort_values(by=['Name', 'Score'], ascending=[True, False])?
medium
A. Rows sorted by Name ascending, then Score descending: Anna(90), Anna(85), Bob(92), Bob(80)
B. Rows sorted by Name descending, then Score ascending: Bob(80), Bob(92), Anna(85), Anna(90)
C. Rows sorted by Score ascending only: Bob(80), Anna(85), Anna(90), Bob(92)
D. Rows sorted by Score descending only: Bob(92), Anna(90), Anna(85), Bob(80)
Solution
Step 1: Sort by 'Name' ascending
Names order: Anna, Anna, Bob, Bob.
Step 2: Within each Name, sort 'Score' descending
For Anna: 90 then 85; for Bob: 92 then 80.
Final Answer:
Rows sorted by Name ascending, then Score descending: Anna(90), Anna(85), Bob(92), Bob(80) -> Option A
Quick Check:
Sort by Name ↑, Score ↓ = Rows sorted by Name ascending, then Score descending: Anna(90), Anna(85), Bob(92), Bob(80) [OK]
Hint: Sort first column ascending, second descending as list [OK]
Common Mistakes:
Mixing ascending and descending order
Sorting only by one column
Confusing order of sorting columns
4.
What is wrong with this code snippet?
df.sort_values(by=['A', 'B'], ascending=True)
Assuming df has columns 'A' and 'B'.
medium
A. The method sort_values does not accept the ascending parameter.
B. The by parameter should be a string, not a list.
C. The ascending parameter should be a list when sorting by multiple columns.
D. The code is correct and will run without errors.
Solution
Step 1: Recall sort_values() parameters
by accepts list of columns, ascending accepts bool (applies to all) or list of bools.
Step 2: Validate the code
The code uses ascending=True, which correctly sorts both columns ascending. No errors.
Final Answer:
The code is correct and will run without errors. -> Option D
Quick Check:
ascending=True valid for multi-column sort [OK]
Hint: ascending=True sorts all columns ascending; use list for mixed orders [OK]
Common Mistakes:
Believing ascending must be a list for multiple columns
Using string instead of list for 'by' parameter
Assuming sort_values lacks ascending parameter
5.
You have a DataFrame df with columns 'City', 'Temperature', and 'Humidity'. You want to sort the data so that cities are in alphabetical order, but within each city, rows with higher temperature come first, and if temperatures are equal, rows with lower humidity come first.
Which sort_values() call achieves this?
hard
A. df.sort_values(by=['City', 'Temperature', 'Humidity'], ascending=[False, True, False])
B. df.sort_values(by=['City', 'Humidity', 'Temperature'], ascending=[True, True, False])
C. df.sort_values(by=['City', 'Temperature', 'Humidity'], ascending=[True, False, True])
D. df.sort_values(by=['Temperature', 'City', 'Humidity'], ascending=[False, True, True])
Solution
Step 1: Sort 'City' alphabetically ascending
Set ascending=True for 'City' to get alphabetical order.
Step 2: Sort 'Temperature' descending within each city
Use ascending=False for 'Temperature' to have higher temperatures first.
Step 3: Sort 'Humidity' ascending when temperatures tie
Use ascending=True for 'Humidity' to get lower humidity first.
Final Answer:
df.sort_values(by=['City', 'Temperature', 'Humidity'], ascending=[True, False, True]) -> Option C