Get startedGet started for free

Sorting by index values

Previously, you changed the order of the rows in a DataFrame by calling .sort_values(). It's also useful to be able to sort by elements in the index. For this, you need to use .sort_index().

pandas is loaded as pd. temperatures_ind has a multi-level index of country and city, and is available.

This exercise is part of the course

Data Manipulation with pandas

View Course

Exercise instructions

  • Sort temperatures_ind by the index values.
  • Sort temperatures_ind by the index values at the "city" level.
  • Sort temperatures_ind by ascending country then descending city.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Sort temperatures_ind by index values
print(____)

# Sort temperatures_ind by index values at the city level
print(____)

# Sort temperatures_ind by country then descending city
print(____)
Edit and Run Code