0
0
Pandasdata~20 mins

Sorting by index 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
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.