Skip to content
Home » pandas Part 6 – the Index Class

pandas Part 6 – the Index Class

Spread the love

In the previous parts of this series we discussed two of the three fundamental data types in pandas, the Series and the DataFrame. In this part we’ll be talking about the third one, the Index.

Both Series and DataFrame objects contain an index, but an Index is also a type on its own. It’s similar to a one-dimensional numpy array, but unlike a numpy array, an Index object is immutable.

You can create an Index from a list of integers:

In [1]:
import numpy as np
import pandas as pd

index = pd.Index([3, 6, 9, 12, 15])
index
Out[1]:
Int64Index([3, 6, 9, 12, 15], dtype='int64')

You can access particular elements using the square bracket notation:

In [4]:
index[1]
Out[4]:
6

Slicing also works like expected:

In [5]:
index[1:4]
Out[5]:
Int64Index([6, 9, 12], dtype='int64')

You can use attributes with pandas Index objects like size, shape, ndim or dtype, which you know from numpy arrays:

In [6]:
index.size, index.shape, index.ndim, index.dtype
Out[6]:
(5, (5,), 1, dtype('int64'))

But you can’t modify the elements of an Index object, because Index objects are immutable. If you try, you get an error:

In [7]:
index[1] = 7
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-7-923c34e5a50d> in <module>
----> 1 index[1] = 7

~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in __setitem__(self, key, value)
   4258 
   4259     def __setitem__(self, key, value):
-> 4260         raise TypeError("Index does not support mutable operations")
   4261 
   4262     def __getitem__(self, key):

TypeError: Index does not support mutable operations

SET OPERATIONS ON INDEX OBJECTS

You can also regard an Index object as an ordered set. You can use all the set operations with Index objects, so you can make unions, differences, symmetric differences and intersections of Index objects:

In [8]:
# let's define two Index objects
index1 = pd.Index([1, 2, 3, 4, 5])
index2 = pd.Index([2, 4, 6, 8])

# let's make a union of the two Index objects
index1.union(index2)
Out[8]:
Int64Index([1, 2, 3, 4, 5, 6, 8], dtype='int64')
In [9]:
# or using the short notation with the pipe operator
index1 | index2
Out[9]:
Int64Index([1, 2, 3, 4, 5, 6, 8], dtype='int64')
In [10]:
# now let's check out the difference of the two object
index1.difference(index2)
Out[10]:
Int64Index([1, 3, 5], dtype='int64')
In [14]:
# and now the symmetric difference, so all elements from index1, which are not in index2
# plus all elements from index2, which are not in index1
index1.symmetric_difference(index2)
Out[14]:
Int64Index([1, 3, 5, 6, 8], dtype='int64')
In [15]:
# Here we can also use the ^ operator
index1 ^ index2
Out[15]:
Int64Index([1, 3, 5, 6, 8], dtype='int64')
In [16]:
# and the intersection
index1.intersection(index2)
Out[16]:
Int64Index([2, 4], dtype='int64')
In [17]:
# or with the & operator
index1 & index2
Out[17]:
Int64Index([2, 4], dtype='int64')

That’s all we need to know about the Index object in pandas for now. In the next part we’ll be talking about data selection in pandas.

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.

Here’s the video version of the article:


Spread the love

Leave a Reply