Get startedGet started for free

Setting and removing indexes

pandas allows you to designate columns as an index. This enables cleaner code when taking subsets (as well as providing more efficient lookup under some circumstances).

In this chapter, you'll be exploring temperatures, a DataFrame of average temperatures in cities around the world. pandas is loaded as pd.

This exercise is part of the course

Data Manipulation with pandas

View Course

Exercise instructions

  • Look at temperatures.
  • Set the index of temperatures to "city", assigning to temperatures_ind.
  • Look at temperatures_ind. How is it different from temperatures?
  • Reset the index of temperatures_ind, keeping its contents.
  • Reset the index of temperatures_ind, dropping its contents.

Hands-on interactive exercise

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

# Look at temperatures
print(____)

# Set the index of temperatures to city
temperatures_ind = ____

# Look at temperatures_ind
print(____)

# Reset the temperatures_ind index, keeping its contents
print(____)

# Reset the temperatures_ind index, dropping its contents
print(____)
Edit and Run Code