0
0
Pandasdata~20 mins

Sorting by values in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sorting Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A
   A  B
2  2  7
1  1  5
0  3  9
B
   A  B
0  3  9
2  2  7
1  1  5
C
   A  B
1  1  5
0  3  9
2  2  7
D
   A  B
1  1  5
2  2  7
0  3  9
Attempts:
2 left
💡 Hint
Remember that sort_values(by='B') sorts the DataFrame by column 'B' in ascending order by default.
data_output
intermediate
2: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)
A
   A  B
2  1  2
0  1  3
3  2  1
1  2  4
B
   A  B
0  1  3
2  1  2
1  2  4
3  2  1
C
   A  B
0  1  3
2  1  2
3  2  1
1  2  4
D
   A  B
2  1  2
0  1  3
1  2  4
3  2  1
Attempts:
2 left
💡 Hint
Sorting by 'A' ascending means rows with A=1 come before A=2. For ties in 'A', sort 'B' descending.
🔧 Debug
advanced
2: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')
AKeyError: 'C'
BValueError: Cannot sort by non-existent column
CTypeError: 'C' is not a valid column
DAttributeError: 'DataFrame' object has no attribute 'C'
Attempts:
2 left
💡 Hint
Check what happens when you try to access a column that does not exist in a DataFrame.
🚀 Application
advanced
2: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)
A
     A    B
1  2  2.0
2  3  1.0
0  1  NaN
B
     A    B
2  3  1.0
1  2  2.0
0  1  NaN
C
     A    B
0  1  NaN
2  3  1.0
1  2  2.0
D
     A    B
0  1  NaN
1  2  2.0
2  3  1.0
Attempts:
2 left
💡 Hint
na_position='first' places missing values at the top when sorting ascending.
🧠 Conceptual
expert
2:00remaining
Effect of inplace parameter in sort_values
What is the effect of setting inplace=True in the sort_values method on a DataFrame?
AThe DataFrame is sorted in place and the method returns None.
BThe DataFrame is sorted and a new sorted DataFrame is returned, original unchanged.
CThe DataFrame is sorted in place and the method returns the sorted DataFrame.
DThe DataFrame is not sorted but a sorted copy is returned.
Attempts:
2 left
💡 Hint
Check the pandas documentation for the behavior of inplace=True in sort_values.