0
0
Pandasdata~20 mins

sort_values() by multiple columns in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of sort_values() by multiple columns
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Sorting a DataFrame by two columns
What is the output of the following code snippet?
Pandas
import pandas as pd

df = pd.DataFrame({
    'Name': ['Anna', 'Bob', 'Charlie', 'Bob', 'Anna'],
    'Age': [25, 30, 35, 22, 25],
    'Score': [88, 92, 85, 95, 90]
})

sorted_df = df.sort_values(by=['Name', 'Age'])
print(sorted_df.reset_index(drop=True))
A
    Name  Age  Score
0   Anna   25     88
1   Anna   25     90
2    Bob   30     92
3    Bob   22     95
4 Charlie   35     85
B
    Name  Age  Score
0   Anna   25     90
1   Anna   25     88
2    Bob   22     95
3    Bob   30     92
4 Charlie   35     85
C
    Name  Age  Score
0   Anna   25     90
1   Anna   25     88
2    Bob   30     92
3    Bob   22     95
4 Charlie   35     85
D
    Name  Age  Score
0   Anna   25     88
1   Anna   25     90
2    Bob   22     95
3    Bob   30     92
4 Charlie   35     85
Attempts:
2 left
💡 Hint
Remember that sort_values sorts first by the first column, then by the second within groups.
data_output
intermediate
1:30remaining
Number of rows after sorting with duplicates
After sorting the DataFrame below by 'Category' and 'Value', how many rows does the sorted DataFrame contain?
Pandas
import pandas as pd

df = pd.DataFrame({
    'Category': ['A', 'B', 'A', 'C', 'B', 'A'],
    'Value': [10, 20, 10, 30, 20, 15]
})

sorted_df = df.sort_values(by=['Category', 'Value'])
print(len(sorted_df))
A6
B5
C4
D7
Attempts:
2 left
💡 Hint
Sorting does not remove rows, it only changes their order.
🔧 Debug
advanced
2:00remaining
Identify the error in sorting by multiple columns
What error will this code raise?
Pandas
import pandas as pd

df = pd.DataFrame({
    'X': [1, 2, 3],
    'Y': [4, 5, 6]
})

sorted_df = df.sort_values(by='X', 'Y')
ASyntaxError: positional argument follows keyword argument
BNo error, sorts by 'X' then 'Y'
CValueError: 'by' must be a string or list of strings
DTypeError: sort_values() got multiple values for argument 'by'
Attempts:
2 left
💡 Hint
Check the function call syntax and argument order.
🚀 Application
advanced
2:00remaining
Sorting with different ascending orders
You want to sort a DataFrame by 'City' ascending and 'Temperature' descending. Which code achieves this?
Pandas
import pandas as pd

df = pd.DataFrame({
    'City': ['Paris', 'Paris', 'London', 'London'],
    'Temperature': [20, 25, 15, 10]
})
Adf.sort_values(by=['City', 'Temperature'], ascending=[False, True])
Bdf.sort_values(by=['City', 'Temperature'], ascending=[True, False])
Cdf.sort_values(by=['City', 'Temperature'], ascending=True)
Ddf.sort_values(by=['City', 'Temperature'], ascending=False)
Attempts:
2 left
💡 Hint
The ascending parameter can be a list matching the columns.
🧠 Conceptual
expert
1:30remaining
Effect of inplace parameter in sort_values()
What is the effect of using inplace=true in the sort_values() method when sorting by multiple columns?
AThe method raises an error because inplace=true is not supported with multiple columns.
BA new sorted DataFrame is returned; the original remains unchanged.
CThe original DataFrame is sorted and changed directly; the method returns null.
DThe method sorts only the first column inplace and ignores others.
Attempts:
2 left
💡 Hint
Recall what inplace=true means for pandas methods.