0
0
Pandasdata~20 mins

Ascending and descending order 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 one column ascending
What is the output of this code snippet sorting the DataFrame by column 'A' in ascending order?
Pandas
import pandas as pd

df = pd.DataFrame({'A': [3, 1, 2], 'B': [9, 8, 7]})
sorted_df = df.sort_values(by='A', ascending=True)
print(sorted_df)
A
   A  B
2  2  7
1  1  8
0  3  9
B
   A  B
1  1  8
2  2  7
0  3  9
C
   A  B
0  3  9
2  2  7
1  1  8
D
   A  B
0  3  9
1  1  8
2  2  7
Attempts:
2 left
💡 Hint
Remember ascending=True means smallest to largest values.
Predict Output
intermediate
2:00remaining
Sorting a DataFrame by multiple columns with mixed order
What is the output of this code sorting by column 'A' ascending and 'B' descending?
Pandas
import pandas as pd

df = pd.DataFrame({'A': [1, 1, 2, 2], 'B': [3, 4, 1, 2]})
sorted_df = df.sort_values(by=['A', 'B'], ascending=[True, False])
print(sorted_df)
A
   A  B
2  2  1
3  2  2
0  1  3
1  1  4
B
   A  B
0  1  3
1  1  4
2  2  1
3  2  2
C
   A  B
3  2  2
2  2  1
1  1  4
0  1  3
D
   A  B
1  1  4
0  1  3
3  2  2
2  2  1
Attempts:
2 left
💡 Hint
First sort by 'A' ascending, then by 'B' descending within each 'A' group.
data_output
advanced
1:30remaining
Number of rows after sorting by descending order
After sorting this DataFrame by column 'score' descending, how many rows remain in the result?
Pandas
import pandas as pd

df = pd.DataFrame({'name': ['Ann', 'Bob', 'Cara'], 'score': [88, 92, 85]})
sorted_df = df.sort_values(by='score', ascending=False)
print(len(sorted_df))
A3
B2
C0
D1
Attempts:
2 left
💡 Hint
Sorting does not remove rows, it only changes order.
🔧 Debug
advanced
1:30remaining
Identify the error in sorting code
What error does this code raise when trying to sort a DataFrame?
Pandas
import pandas as pd

df = pd.DataFrame({'X': [1, 2], 'Y': [3, 4]})
sorted_df = df.sort_values(by='Z')
AKeyError: 'Z'
BTypeError: unsupported operand type(s)
CValueError: axis must be 0 or 1
DAttributeError: 'DataFrame' object has no attribute 'sort_values'
Attempts:
2 left
💡 Hint
Check if the column name exists in the DataFrame.
🚀 Application
expert
2:30remaining
Sorting and resetting index in a DataFrame
After sorting the DataFrame by column 'age' descending, which option correctly resets the index to default integers?
Pandas
import pandas as pd

df = pd.DataFrame({'name': ['Tom', 'Jerry', 'Spike'], 'age': [5, 3, 7]})
sorted_df = df.sort_values(by='age', ascending=False)
A
sorted_df.reset_index(drop=True)
print(sorted_df)
B
sorted_df.reset_index(drop=False, inplace=True)
print(sorted_df)
C
sorted_df.reset_index(drop=True, inplace=True)
print(sorted_df)
D
sorted_df.reset_index(inplace=False)
print(sorted_df)
Attempts:
2 left
💡 Hint
To remove old index and update in place, use drop=True and inplace=True.