Indexing in pandas DataFrames

In this section, we will explore how to set an index and use it for data analysis in pandas. We will learn how to set an index on the DataFrame after reading in the data, as well as while reading in data. We'll also see how to use this index for data selection.

As always, we start by importing the pandas module into our Jupyter notebook:

import pandas as pd

We then read in our dataset:

data = pd.read_csv('data-titanic.csv')

The following is how our default index looks like right now, which is a numeric index starting from 0:


Let's set it to a column of our choice. Here, we use the set_index method ...

Get Mastering Exploratory Analysis with pandas now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.