Bird
Raised Fist0
Pandasdata~20 mins

sort_index() for index sorting 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
What is the output of sorting a DataFrame by index with ascending=False?
Given the DataFrame df with index [3, 1, 2] and values ["a", "b", "c"], what is the result of df.sort_index(ascending=False)?
Pandas
import pandas as pd
df = pd.DataFrame({'value': ['a', 'b', 'c']}, index=[3, 1, 2])
result = df.sort_index(ascending=False)
A{3: 'a', 2: 'c', 1: 'b'}
B{1: 'b', 2: 'c', 3: 'a'}
C{2: 'c', 3: 'a', 1: 'b'}
D{1: 'b', 3: 'a', 2: 'c'}
Attempts:
2 left
💡 Hint
Remember that ascending=False sorts from highest index to lowest.
📝 Syntax
intermediate
2:00remaining
Which option correctly sorts a DataFrame by its index in place?
You want to sort the DataFrame df by its index and modify it directly without creating a new object. Which code does this correctly?
Pandas
import pandas as pd
df = pd.DataFrame({'val': [10, 20, 30]}, index=[2, 0, 1])
Adf.sort_index(inplace=False)
Bdf.sort_index(inplace=True)
Cdf.sort_index(inplace=1)
Ddf.sort_index(in_place=True)
Attempts:
2 left
💡 Hint
Check the exact parameter name and its boolean value.
❓ optimization
advanced
2:30remaining
How to efficiently sort a MultiIndex DataFrame by the second level index?
Given a DataFrame df with a MultiIndex of two levels, which code sorts the DataFrame by the second level of the index only?
Pandas
import pandas as pd
index = pd.MultiIndex.from_tuples([(1, 'b'), (2, 'a'), (1, 'a')], names=['num', 'char'])
df = pd.DataFrame({'val': [10, 20, 30]}, index=index)
Adf.sort_index(level=1)
Bdf.sort_index(level=0)
Cdf.sort_index(axis=1, level=1)
Ddf.sort_index(by=1)
Attempts:
2 left
💡 Hint
The 'level' parameter specifies which index level to sort by.
🔧 Debug
advanced
2:00remaining
Why does this sort_index call raise an error?
Consider the code: df.sort_index(axis=2). Why does this raise an error?
Pandas
import pandas as pd
df = pd.DataFrame({'a': [1, 2], 'b': [3, 4]})
df.sort_index(axis=2)
AThe DataFrame columns are not sorted alphabetically by default.
BThe DataFrame has no index to sort.
Csort_index requires a 'level' parameter for sorting.
DAxis 2 does not exist for a DataFrame; valid axes are 0 (index) and 1 (columns).
Attempts:
2 left
💡 Hint
Check the valid axis values for DataFrame methods.
🧠 Conceptual
expert
3:00remaining
What happens when you sort a DataFrame with a non-unique index using sort_index()?
If a DataFrame has duplicate index values, what is the behavior of df.sort_index()?
AIt removes duplicate index rows before sorting.
BIt raises an error because index values must be unique to sort.
CIt sorts all rows by index values, keeping duplicates in order as they appear.
DIt sorts only the unique index values and drops duplicates.
Attempts:
2 left
💡 Hint
Think about whether sorting requires unique index values.

Practice

(1/5)
1. What does the sort_index() function do in pandas?
easy
A. It filters rows based on a condition.
B. It sorts the DataFrame by its values.
C. It sorts the DataFrame by its index labels.
D. It resets the index to default integers.

Solution

  1. Step 1: Understand the purpose of sort_index()

    This function organizes the DataFrame by sorting its row or column labels (index), not the data values.
  2. Step 2: Compare options

    Only It sorts the DataFrame by its index labels. correctly describes sorting by index labels. Options B, C, and D describe other functions.
  3. Final Answer:

    It sorts the DataFrame by its index labels. -> Option C
  4. Quick Check:

    sort_index() = sort by index [OK]
Hint: Remember: sort_index() sorts labels, not data values [OK]
Common Mistakes:
  • Confusing sort_index() with sort_values()
  • Thinking it filters rows
  • Assuming it resets the index
2. Which of the following is the correct syntax to sort a DataFrame df by its index in descending order?
easy
A. df.sort_index(order='desc')
B. df.sort_index(ascending=False)
C. df.sort_index(descending=True)
D. df.sort_index(reverse=True)

Solution

  1. Step 1: Recall the parameter for sorting order

    The correct parameter is ascending, which accepts True or False.
  2. Step 2: Check each option

    Only df.sort_index(ascending=False) uses ascending=False correctly. Options A, B, and D use invalid parameters.
  3. Final Answer:

    df.sort_index(ascending=False) -> Option B
  4. Quick Check:

    Use ascending=False for descending sort [OK]
Hint: Use ascending=False to sort index descending [OK]
Common Mistakes:
  • Using invalid parameter names like descending or reverse
  • Confusing ascending with descending
  • Missing parentheses after sort_index
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?
medium
A. [1, 2, 3]
B. [3, 2, 1]
C. ["a", "b", "c"]
D. [2, 3, 1]

Solution

  1. Step 1: Understand the initial index

    The DataFrame index is [3, 1, 2], unordered.
  2. Step 2: Apply sort_index()

    This sorts the index labels in ascending order: [1, 2, 3].
  3. Final Answer:

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

    sort_index() sorts index ascending by default [OK]
Hint: sort_index() sorts index ascending by default [OK]
Common Mistakes:
  • Confusing index labels with values
  • Expecting descending order without specifying
  • Thinking values get sorted instead of index
4. What is wrong with this code snippet to sort the columns of DataFrame df by their labels?
df.sort_index(axis=1, inplace=False)
medium
A. It does not modify df because inplace=False is used.
B. The axis parameter should be 0 to sort columns.
C. sort_index() cannot sort columns, only rows.
D. inplace must be True to return a sorted DataFrame.

Solution

  1. Step 1: Understand inplace parameter

    With inplace=False, the original DataFrame df is not changed; a sorted copy is returned.
  2. Step 2: Check axis parameter

    axis=1 correctly specifies sorting columns, so no error there.
  3. Step 3: Identify the issue

    Since the returned sorted DataFrame is not assigned, df remains unsorted.
  4. Final Answer:

    It does not modify df because inplace=False is used. -> Option A
  5. Quick Check:

    inplace=False returns new DataFrame, original unchanged [OK]
Hint: Use inplace=True or assign result to keep changes [OK]
Common Mistakes:
  • Assuming inplace=False modifies original DataFrame
  • Confusing axis=0 and axis=1 for columns
  • Thinking sort_index() can't sort columns
5. You have a DataFrame df with a MultiIndex on rows. How can you sort the DataFrame by the second level of the index using sort_index()?
hard
A. df.sort_index(index=1)
B. df.sort_index(axis=1, level=1)
C. df.sort_index(by=1)
D. df.sort_index(level=1)

Solution

  1. Step 1: Understand MultiIndex sorting

    To sort by a specific level in a MultiIndex, use the level parameter with the level number.
  2. Step 2: Check each option

    df.sort_index(level=1) correctly uses level=1 to sort by the second index level. Options A, B, and C use invalid parameters.
  3. Final Answer:

    df.sort_index(level=1) -> Option D
  4. Quick Check:

    Use level parameter to sort MultiIndex levels [OK]
Hint: Use level=1 to sort by second index level [OK]
Common Mistakes:
  • Using axis=1 to sort rows
  • Using invalid parameters like by or index
  • Not specifying level for MultiIndex sorting
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