0
0
Pandasdata~10 mins

sort_values() by multiple columns in Pandas - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to sort the DataFrame by the 'age' column in ascending order.

Pandas
df_sorted = df.sort_values(by=[1])
Drag options to blanks, or click blank then click option'
A'age'
B'salary'
C'name'
D'height'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a column name that does not exist in the DataFrame.
Not putting the column name inside quotes.
2fill in blank
medium

Complete the code to sort the DataFrame by 'age' ascending and then by 'salary' descending.

Pandas
df_sorted = df.sort_values(by=['age', [1]], ascending=[True, False])
Drag options to blanks, or click blank then click option'
A'salary'
B'height'
C'name'
D'department'
Attempts:
3 left
💡 Hint
Common Mistakes
Not passing a list to the by parameter.
Mixing up the order of columns in the list.
3fill in blank
hard

Fix the error in the code to correctly sort by 'department' ascending and 'age' descending.

Pandas
df_sorted = df.sort_values(by=['department', [1]], ascending=[True, False])
Drag options to blanks, or click blank then click option'
A'salary'
B'age'
C'name'
D'height'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong column name that does not exist.
Not matching the order of columns with the ascending list.
4fill in blank
hard

Fill both blanks to create a dictionary that sorts by 'age' ascending and 'salary' descending.

Pandas
df_sorted = df.sort_values(by=[1], ascending=[2])
Drag options to blanks, or click blank then click option'
A['age', 'salary']
B[True, False]
C[False, True]
D['salary', 'age']
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the order of columns and booleans.
Using a single boolean instead of a list for multiple columns.
5fill in blank
hard

Fill all three blanks to create a sorted DataFrame by 'department' ascending, 'age' descending, and 'salary' ascending.

Pandas
df_sorted = df.sort_values(by=[[1], [2], [3]], ascending=[True, False, True])
Drag options to blanks, or click blank then click option'
A'department'
B'age'
C'salary'
D'name'
Attempts:
3 left
💡 Hint
Common Mistakes
Putting the columns in the wrong order.
Not matching the length of by and ascending lists.