Challenge - 5 Problems
Sorting Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Sorting a DataFrame by a single column
What is the output of this code snippet that sorts a DataFrame by column 'B' in ascending order?
Pandas
import pandas as pd df = pd.DataFrame({'A': [3, 1, 2], 'B': [9, 5, 7]}) sorted_df = df.sort_values(by='B') print(sorted_df)
Attempts:
2 left
💡 Hint
Remember that sort_values(by='B') sorts the DataFrame by column 'B' in ascending order by default.
✗ Incorrect
The DataFrame is sorted by column 'B' values: 5, 7, 9. The rows are reordered accordingly, but the original indices are preserved.
❓ data_output
intermediate2:00remaining
Sorting by multiple columns with different orders
Given the DataFrame below, what is the output after sorting by column 'A' ascending and then by column 'B' descending?
Pandas
import pandas as pd df = pd.DataFrame({'A': [1, 2, 1, 2], 'B': [3, 4, 2, 1]}) sorted_df = df.sort_values(by=['A', 'B'], ascending=[True, False]) print(sorted_df)
Attempts:
2 left
💡 Hint
Sorting by 'A' ascending means rows with A=1 come before A=2. For ties in 'A', sort 'B' descending.
✗ Incorrect
Rows with A=1 are sorted by B descending: 3 then 2. Rows with A=2 sorted by B descending: 4 then 1.
🔧 Debug
advanced2:00remaining
Identify the error in sorting by values
What error does this code raise when trying to sort a DataFrame by a non-existent column 'C'?
Pandas
import pandas as pd df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]}) sorted_df = df.sort_values(by='C')
Attempts:
2 left
💡 Hint
Check what happens when you try to access a column that does not exist in a DataFrame.
✗ Incorrect
Pandas raises a KeyError when you try to sort by a column name that is not present in the DataFrame.
🚀 Application
advanced2:00remaining
Sorting a DataFrame with missing values
You have this DataFrame with missing values in column 'B'. What is the output after sorting by 'B' with na_position='first'?
Pandas
import pandas as pd import numpy as np df = pd.DataFrame({'A': [1, 2, 3], 'B': [np.nan, 2, 1]}) sorted_df = df.sort_values(by='B', na_position='first') print(sorted_df)
Attempts:
2 left
💡 Hint
na_position='first' places missing values at the top when sorting ascending.
✗ Incorrect
The NaN value in row 0 is placed first, then rows sorted by B ascending: 1.0 then 2.0.
🧠 Conceptual
expert2:00remaining
Effect of inplace parameter in sort_values
What is the effect of setting inplace=True in the sort_values method on a DataFrame?
Attempts:
2 left
💡 Hint
Check the pandas documentation for the behavior of inplace=True in sort_values.
✗ Incorrect
When inplace=True, the original DataFrame is modified and the method returns None.