Bird
Raised Fist0
Pandasdata~20 mins

Ascending and descending order in Pandas - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does setting ascending=False do when sorting a pandas DataFrame?
easy
A. Sorts the data from largest to smallest values
B. Sorts the data from smallest to largest values
C. Leaves the data unsorted
D. Sorts the data alphabetically only

Solution

  1. Step 1: Understand the ascending parameter

    The ascending parameter controls the order of sorting: True means smallest to largest, False means largest to smallest.
  2. Step 2: Apply the meaning of ascending=False

    Setting ascending=False sorts the data from the largest values down to the smallest values.
  3. Final Answer:

    Sorts the data from largest to smallest values -> Option A
  4. Quick Check:

    ascending=False = largest to smallest [OK]
Hint: False means descending order, largest first [OK]
Common Mistakes:
  • Confusing ascending=False with ascending=True
  • Thinking it sorts alphabetically only
  • Assuming it leaves data unsorted
2. Which of the following is the correct syntax to sort a DataFrame df by column 'age' in ascending order?
easy
A. df.sort('age', ascending=True)
B. df.sort_values('age', ascending=True)
C. df.sort_values(by='age', ascending=True)
D. df.sort_values(by='age', ascending=False)

Solution

  1. Step 1: Recall the correct pandas sorting method

    The correct method to sort a DataFrame by column is sort_values() with the parameter by='column_name'.
  2. Step 2: Check the ascending parameter and syntax

    To sort ascending, use ascending=True. df.sort_values(by='age', ascending=True) uses the correct method and parameters.
  3. Final Answer:

    df.sort_values(by='age', ascending=True) -> Option C
  4. Quick Check:

    sort_values(by=col, ascending=True) is correct [OK]
Hint: Use sort_values(by=col, ascending=True) for ascending sort [OK]
Common Mistakes:
  • Using df.sort instead of df.sort_values
  • Mixing ascending True/False values
  • Omitting 'by=' keyword
3. Given the DataFrame:
import pandas as pd
df = pd.DataFrame({'name': ['Anna', 'Bob', 'Cara'], 'score': [88, 92, 85]})
print(df.sort_values(by='score', ascending=False))

What will be the output?
medium
A. Rows sorted by score from highest to lowest
B. Error because ascending must be True
C. Rows sorted by name alphabetically
D. Rows sorted by score from lowest to highest

Solution

  1. Step 1: Understand the sorting code

    The code sorts the DataFrame by the 'score' column with ascending=False, meaning from highest to lowest score.
  2. Step 2: Predict the sorted order

    Scores are 88, 92, 85. Sorted descending: 92 (Bob), 88 (Anna), 85 (Cara).
  3. Final Answer:

    Rows sorted by score from highest to lowest -> Option A
  4. Quick Check:

    ascending=False sorts descending [OK]
Hint: False sorts descending, highest first [OK]
Common Mistakes:
  • Thinking ascending=False sorts ascending
  • Confusing sorting by name vs score
  • Expecting an error from ascending=False
4. What is wrong with this code snippet?
df.sort_values(by='height', ascending='False')
medium
A. sort_values does not accept ascending parameter
B. The column name must be a list, not a string
C. No error, code is correct
D. ascending should be a boolean, not a string

Solution

  1. Step 1: Check the type of ascending parameter

    The ascending parameter expects a boolean value: True or False, not a string.
  2. Step 2: Identify the error in the code

    Here, ascending='False' is a string, which will cause unexpected behavior or error.
  3. Final Answer:

    ascending should be a boolean, not a string -> Option D
  4. Quick Check:

    ascending must be True/False boolean [OK]
Hint: Use True/False without quotes for ascending [OK]
Common Mistakes:
  • Passing 'False' as string instead of boolean
  • Thinking column name must be list always
  • Assuming no error with wrong ascending type
5. You have a DataFrame df with columns 'city' and 'temperature'. You want to sort it first by 'city' alphabetically ascending, then by 'temperature' descending. Which code does this correctly?
hard
A. df.sort_values(by=['city', 'temperature'], ascending=False)
B. df.sort_values(by=['city', 'temperature'], ascending=[True, False])
C. df.sort_values(by=['temperature', 'city'], ascending=[False, True])
D. df.sort_values(by=['city', 'temperature'], ascending=[False, True])

Solution

  1. Step 1: Understand multi-column sorting

    To sort by multiple columns, pass a list to by and a list of booleans to ascending matching each column.
  2. Step 2: Match sorting order to columns

    We want 'city' ascending (True) and 'temperature' descending (False), so ascending=[True, False].
  3. Final Answer:

    df.sort_values(by=['city', 'temperature'], ascending=[True, False]) -> Option B
  4. Quick Check:

    Multi-column sort uses list of booleans [OK]
Hint: Use ascending list matching columns for multi-sort [OK]
Common Mistakes:
  • Using single boolean for multi-column ascending
  • Swapping column order in by list
  • Mixing ascending True/False order
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard