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
Exercise instructions
- Look at
temperatures
. - Set the index of
temperatures
to"city"
, assigning totemperatures_ind
. - Look at
temperatures_ind
. How is it different fromtemperatures
? - 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(____)