Bird
Raised Fist0
Pandasdata~20 mins

Sorting by index in Pandas - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Index Sorting Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
Sorting a DataFrame by index ascending
Given the DataFrame below, what is the output after sorting by index in ascending order?

import pandas as pd

df = pd.DataFrame({
  'A': [3, 1, 2],
  'B': [9, 8, 7]
}, index=['c', 'a', 'b'])

sorted_df = df.sort_index()
Pandas
import pandas as pd

df = pd.DataFrame({
  'A': [3, 1, 2],
  'B': [9, 8, 7]
}, index=['c', 'a', 'b'])

sorted_df = df.sort_index()
print(sorted_df)
A
   A  B
a  1  8
b  2  7
c  3  9
B
   A  B
c  3  9
b  2  7
a  1  8
C
   A  B
b  2  7
a  1  8
c  3  9
D
   A  B
a  3  9
b  1  8
c  2  7
Attempts:
2 left
💡 Hint
Sorting by index with default parameters sorts in ascending order alphabetically.
❓ query_result
intermediate
2:00remaining
Sorting a DataFrame by index descending
What is the output of sorting the following DataFrame by index in descending order?

import pandas as pd

df = pd.DataFrame({
  'X': [10, 20, 30],
  'Y': [100, 200, 300]
}, index=[2, 1, 3])

sorted_df = df.sort_index(ascending=False)
print(sorted_df)
Pandas
import pandas as pd

df = pd.DataFrame({
  'X': [10, 20, 30],
  'Y': [100, 200, 300]
}, index=[2, 1, 3])

sorted_df = df.sort_index(ascending=False)
print(sorted_df)
A
    X    Y
1  20  200
2  10  100
3  30  300
B
    X    Y
3  30  300
2  10  100
1  20  200
C
    X    Y
3  30  300
1  20  200
2  10  100
D
    X    Y
2  10  100
3  30  300
1  20  200
Attempts:
2 left
💡 Hint
Descending order means largest index first.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in sorting by index
Which option contains a syntax error when trying to sort a DataFrame by index?
Pandas
import pandas as pd
df = pd.DataFrame({'A':[1,2]}, index=[1,0])
Adf.sort_index(ascending=True)
Bdf.sort_index()
Cdf.sort_index(ascending=False)
Ddf.sort_index(ascending==False)
Attempts:
2 left
💡 Hint
Check the use of assignment vs comparison operators in function arguments.
❓ query_result
advanced
2:30remaining
Sorting a MultiIndex DataFrame by level
Given the MultiIndex DataFrame below, what is the output after sorting by the second level of the index?

import pandas as pd

index = pd.MultiIndex.from_tuples([('a', 2), ('a', 1), ('b', 1), ('b', 2)])
df = pd.DataFrame({'val': [10, 20, 30, 40]}, index=index)
sorted_df = df.sort_index(level=1)
print(sorted_df)
Pandas
import pandas as pd

index = pd.MultiIndex.from_tuples([('a', 2), ('a', 1), ('b', 1), ('b', 2)])
df = pd.DataFrame({'val': [10, 20, 30, 40]}, index=index)
sorted_df = df.sort_index(level=1)
print(sorted_df)
A
       val
(a, 1) 20
(b, 1) 30
(a, 2) 10
(b, 2) 40
B
       val
(a, 2) 10
(a, 1) 20
(b, 1) 30
(b, 2) 40
C
       val
(b, 1) 30
(a, 1) 20
(a, 2) 10
(b, 2) 40
D
       val
(a, 1) 20
(a, 2) 10
(b, 1) 30
(b, 2) 40
Attempts:
2 left
💡 Hint
Sorting by level=1 sorts by the second index value across all first-level groups.
🧠 Conceptual
expert
2:30remaining
Effect of inplace parameter in sort_index
What is the effect of using inplace=True when calling df.sort_index() on a DataFrame?
AThe DataFrame is sorted by index but the changes are only temporary and lost after the next operation.
BA new sorted DataFrame is returned and the original DataFrame remains unchanged.
CThe DataFrame is sorted by index and the original DataFrame is modified directly without returning a new DataFrame.
DThe DataFrame is sorted by index and a copy is returned, but the original DataFrame is also modified.
Attempts:
2 left
💡 Hint
Consider what inplace means in pandas methods.

Practice

(1/5)
1. What does sorting by index in pandas do?
easy
A. It changes the data values to alphabetical order.
B. It filters rows based on a condition.
C. It removes duplicate rows from the DataFrame.
D. It arranges data rows or columns based on their labels.

Solution

  1. Step 1: Understand the meaning of sorting by index

    Sorting by index means arranging the data based on the row or column labels, not the data values.
  2. Step 2: Compare options with the definition

    Only It arranges data rows or columns based on their labels. correctly describes sorting by index as arranging rows or columns by their labels.
  3. Final Answer:

    It arranges data rows or columns based on their labels. -> Option D
  4. Quick Check:

    Sorting by index = arrange by labels [OK]
Hint: Sorting by index means ordering by labels, not values [OK]
Common Mistakes:
  • Confusing sorting by index with sorting by values
  • Thinking it filters or removes data
  • Assuming it changes data values
2. Which of the following is the correct syntax to sort a DataFrame df by its row index in descending order without changing the original DataFrame?
easy
A. df.sort_index(ascending=False, inplace=False)
B. df.sort_index(axis=1, ascending=False)
C. df.sort_index(ascending=True, inplace=False)
D. df.sort_index(ascending=False, inplace=True)

Solution

  1. Step 1: Identify parameters for sorting by index

    To sort by row index descending, use ascending=False. To avoid changing original, use inplace=False.
  2. Step 2: Check each option

    df.sort_index(ascending=False, inplace=False) uses ascending=False and inplace=False, which matches the requirement. df.sort_index(ascending=False, inplace=True) changes original (inplace=True). df.sort_index(ascending=True, inplace=False) sorts ascending. df.sort_index(axis=1, ascending=False) sorts columns (axis=1).
  3. Final Answer:

    df.sort_index(ascending=False, inplace=False) -> Option A
  4. Quick Check:

    Descending + no inplace = ascending=False, inplace=False [OK]
Hint: Use inplace=False to keep original unchanged [OK]
Common Mistakes:
  • Using inplace=True when original should not change
  • Confusing ascending=True with descending order
  • Sorting columns instead of rows by mistake
3. Given the DataFrame df with index labels [3, 1, 2] and values ["a", "b", "c"], what will df.sort_index() return as the index order?
medium
A. [a, b, c]
B. [1, 2, 3]
C. [3, 2, 1]
D. [c, b, a]

Solution

  1. Step 1: Understand default sort_index behavior

    By default, sort_index sorts row labels in ascending order.
  2. Step 2: Apply sorting to index [3, 1, 2]

    Sorting ascending gives [1, 2, 3]. Values remain associated with their labels.
  3. Final Answer:

    [1, 2, 3] -> Option B
  4. Quick Check:

    Default sort_index sorts index ascending [OK]
Hint: Default sort_index sorts index ascending [OK]
Common Mistakes:
  • Confusing index labels with values
  • Assuming sort_index sorts values instead of index
  • Thinking sort_index reverses order by default
4. You run df.sort_index(axis=2) on a DataFrame but get an error. What is the likely cause?
medium
A. axis=2 is invalid; axis must be 0 or 1.
B. sort_index does not accept axis parameter.
C. DataFrame index labels are missing.
D. inplace parameter is required.

Solution

  1. Step 1: Check valid axis values for sort_index

    sort_index accepts axis=0 (rows) or axis=1 (columns) only.
  2. Step 2: Identify error cause with axis=2

    axis=2 is invalid and causes an error.
  3. Final Answer:

    axis=2 is invalid; axis must be 0 or 1. -> Option A
  4. Quick Check:

    axis must be 0 or 1 in sort_index [OK]
Hint: axis can only be 0 (rows) or 1 (columns) [OK]
Common Mistakes:
  • Using axis values other than 0 or 1
  • Assuming sort_index ignores invalid axis
  • Forgetting axis parameter is optional but must be valid
5. You have a DataFrame df with a multi-level index (two levels). How do you sort df by the second level of the index in ascending order without changing the original DataFrame?
hard
A. df.sort_values(level=1, ascending=True)
B. df.sort_index(level=0, ascending=True, inplace=True)
C. df.sort_index(level=1, ascending=True, inplace=False)
D. df.sort_index(axis=1, level=1)

Solution

  1. Step 1: Understand sorting multi-level index

    Use sort_index with level parameter to specify which index level to sort.
  2. Step 2: Choose correct parameters

    To sort by second level (level=1), ascending order, and keep original unchanged, use inplace=False.
  3. Final Answer:

    df.sort_index(level=1, ascending=True, inplace=False) -> Option C
  4. Quick Check:

    Sort multi-level index by level=1, inplace=False [OK]
Hint: Use level parameter to sort multi-level index [OK]
Common Mistakes:
  • Using sort_values instead of sort_index for index sorting
  • Sorting wrong index level (level=0 instead of 1)
  • Using inplace=True when original should not change
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