Indexing Operations

Learn how to manipulate indexes of a Series or a DataFrame.

Indexing is an overloaded term in the pandas world. Both a Series and a DataFrame have an index (the labels down the left side for each row). In addition, both types support the Python indexing operator ([]). But that’s not all. They both have attributes (loc and iloc) that we can index against (using the Python indexing operator).

Prepping the data and renaming the index

To help explain the various operations, we’ll take the automobile mileage data Series with the city miles per gallon values and insert each car’s make as the index. This is because many operations work on the index position while others work on the index label. If these are both integer values, it can be a little confusing, but it becomes clearer if the index has string labels.

We’ll use the rename method to change the index labels. We can pass in a dictionary to map the previous index label to the new label:

Get hands-on with 1200+ tech skills courses.