Sort by two columns
A pandas exercise from Pandasdrill. Solve it with a Python editor in your browser running real pandas, and it is checked the moment you submit.
The task
Sort `emp` by department A to Z, and within each department by salary from highest to lowest. Keep the original row labels.
What this exercise teaches
With two sort keys, pandas sorts by the first and breaks ties with the second. Passing ascending=[True, False] sets the direction for each key separately.
A hint if you get stuck
sort_values accepts a list of columns and a matching list of True/False values for ascending.
This exercise is part of Pro
Pro unlocks all 45 exercises, with worked solutions and progress saved to your account. The free exercises work without an account.
You create a free account first, so Pro is attached to it.
Monthly is charged today and then every month until you cancel, in one click from your account page. Lifetime is one payment with no renewals. Full refund within 14 days of paying.