0
0
Pandasdata~20 mins

sort_values() by single column in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sort Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of sorting DataFrame by one column ascending
What is the output of this code snippet that sorts the DataFrame by column 'Age' in ascending order?
Pandas
import pandas as pd

df = pd.DataFrame({
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [25, 30, 20]
})
sorted_df = df.sort_values(by='Age')
print(sorted_df)
A
     Name  Age
2  Charlie   20
0    Alice   25
1      Bob   30
B
     Name  Age
0    Alice   25
1      Bob   30
2  Charlie   20
C
     Name  Age
1      Bob   30
0    Alice   25
2  Charlie   20
D
     Name  Age
2  Charlie   20
1      Bob   30
0    Alice   25
Attempts:
2 left
💡 Hint
Remember that sort_values(by='Age') sorts the rows by the 'Age' column in ascending order by default.
data_output
intermediate
1:30remaining
Number of rows after sorting by a single column
After sorting this DataFrame by the 'Score' column, how many rows does the resulting DataFrame have?
Pandas
import pandas as pd

df = pd.DataFrame({
    'Student': ['Ann', 'Ben', 'Cara', 'Dan'],
    'Score': [88, 92, 85, 90]
})
sorted_df = df.sort_values(by='Score')
print(len(sorted_df))
A0
B3
C4
D1
Attempts:
2 left
💡 Hint
Sorting does not remove or add rows; it only changes their order.
🔧 Debug
advanced
2:00remaining
Identify the error in sorting by a single column
What error does this code produce when trying to sort the DataFrame by column 'Height'?
Pandas
import pandas as pd

df = pd.DataFrame({
    'Name': ['Eva', 'Frank'],
    'Weight': [55, 70]
})
sorted_df = df.sort_values(by='Height')
AValueError: Cannot sort by multiple columns
BNo error, returns sorted DataFrame
CTypeError: unsupported operand type(s) for <
DKeyError: 'Height'
Attempts:
2 left
💡 Hint
Check if the column name exists in the DataFrame before sorting.
🧠 Conceptual
advanced
1:30remaining
Effect of inplace=True in sort_values()
What is the effect of using inplace=True in the sort_values() method on a DataFrame?
AThe method raises an error because inplace is not a valid argument.
BThe original DataFrame is sorted and updated without creating a new DataFrame.
CThe DataFrame is sorted but the changes are lost after the method call.
DA new sorted DataFrame is returned, and the original remains unchanged.
Attempts:
2 left
💡 Hint
inplace=True means the operation modifies the original object directly.
🚀 Application
expert
2:30remaining
Sorting and resetting index in a DataFrame
Given this DataFrame, which option correctly sorts by 'Salary' descending and resets the index to start from 0?
Pandas
import pandas as pd

df = pd.DataFrame({
    'Employee': ['John', 'Jane', 'Doe'],
    'Salary': [50000, 60000, 55000]
})
Adf.sort_values(by='Salary', ascending=False).reset_index(drop=True)
Bdf.sort_values(by='Salary', ascending=True).reset_index()
Cdf.reset_index().sort_values(by='Salary', ascending=False)
Ddf.sort_values('Salary', ascending=False, inplace=True).reset_index(drop=True)
Attempts:
2 left
💡 Hint
Remember to sort descending and reset index without keeping old index as a column.