0
0
Pandasdata~20 mins

sort_index() for index sorting in Pandas - Practice Problems & Coding Challenges

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