Indexing in Pandas

Summary : in this tutorial, you’ll learn about the Index object – an important part of pandas.

The Index object

As you already know, Pandas is built on the back of NumPy. Therefore, the Series and DataFrames intherits some important parts from it.

Each axis of a Series and a DataFrame has an Index object that labels the values.

There are many different types of Index objects, but all Index objects, except for the MultiIndex, are single-dimensional data structures, which somehow resemblances a Python list.

For example, we can examine column contents with df.columns.

import pandas as pd
# Import data from Open Brewery DB
df = pd.read_csv("https://github.com/openbrewerydb/openbrewerydb/raw/master/data/united-states/hawaii.csv")

df.columns
Index(['obdb_id', 'name', 'brewery_type', 'street', 'address_2', 'address_3',
       'city', 'state', 'county_province', 'postal_code', 'website_url',
       'phone', 'created_at', 'updated_at', 'country', 'longitude', 'latitude',
       'tags'],
      dtype='object')

Or, you can count total rows in the DataFrame using df.index.

df.index
RangeIndex(start=0, stop=23, step=1)

Index basic operations

The Index object can be sliced or extracted using square brackets operator.

import pandas as pd
# Import data from Open Brewery DB
df = pd.read_csv("https://github.com/openbrewerydb/openbrewerydb/raw/master/data/united-states/hawaii.csv")

df.columns[5]
# Returns
'address_3'

df.columns[1:4]
# Returns
Index(['name', 'brewery_type', 'street'], dtype='object')

The Index objects also share many methods as the Series and DataFrames, such as min(), max() or sum().

But if you try to modify a value inside an Index object, you’ll encounter TypeError as Index objects are immutable.

import pandas as pd
# Import data from Open Brewery DB
df = pd.read_csv("https://github.com/openbrewerydb/openbrewerydb/raw/master/data/united-states/hawaii.csv")

df.columns[1] = 'monkeybean'
TypeError: Index does not support mutable operations

Note that more information on the Index class can be found at Pandas documentation.

Leave a Comment