Skip to content
Home » pandas Part 8 – The loc and iloc Indexers

pandas Part 8 – The loc and iloc Indexers

Spread the love

In the previous part we saw an interesting example of slicing. Let’s use the same example again:

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

nums = pd.Series([2.35, 4.11, 0.87, 2.76, 3.12, 5.79],
                index = ['A', 'B', 'C', 'D', 'E', 'F'])

nums
Out[1]:
A    2.35
B    4.11
C    0.87
D    2.76
E    3.12
F    5.79
dtype: float64

Now, we can slice the Series object using the explicit label indices:

In [2]:
nums['B':'E']
Out[2]:
B    4.11
C    0.87
D    2.76
E    3.12
dtype: float64

or the implicit positional indices:

In [3]:
nums[1:4]
Out[3]:
B    4.11
C    0.87
D    2.76
dtype: float64

As you remember, the second index is included if we use label indices, but not if we use the positional ones. This may cause confusion if the label indices are integers too. Have a look at this modified example:

In [14]:
nums = pd.Series([2.35, 4.11, 0.87, 2.76, 3.12, 5.79],
                index = [1, 2, 3, 4, 5, 6])

First, let’s try to access an element by index. Suppose we’re interested in the element with the value 4.11. It’s label index is 2, but it’s implicit positional index is 1. So, which one should we use? Let’s find out:

In [15]:
# using the label index
nums[2]
Out[15]:
4.11
In [16]:
# using the positional index
nums[1]
Out[16]:
2.35

Looks like the label index rules. So, if we want to make a slice from the element with the value 4.11 to the element with the value 2.76, including the latter, we should use the label indices, remembering that the second one is included, right? Let’s check it out:

In [17]:
nums[2:4]
Out[17]:
3    0.87
4    2.76
dtype: float64

Disappointed? Looks like this time the positional, not the label index rules. So, when we index a Series object, the label index is used, but when we slice it, the positional index is the one that matters. Pretty confusing. So, is there a way to always use only the label index or always use the positional one? There is. This is where indexers come in really handy, and in particular the indexers loc and iloc.

The indexers are attributes that tell the Series object how it should be indexed. Here’s how they work:

loc – always use the explicit label index iloc – always use the implicit positional integer index

So, it’s always better to use the indexers to make our code clear, especially if the indices are integers. And now have a look at the indexers in action:

In [19]:
# here's our nums Series object again
nums = pd.Series([2.35, 4.11, 0.87, 2.76, 3.12, 5.79],
                index = [1, 2, 3, 4, 5, 6])

# just to see the difference clearly, let's index it without the indexers again
nums[3]
Out[19]:
0.87
In [20]:
# and let's slice it without the indexers
nums[2:4]
Out[20]:
3    0.87
4    2.76
dtype: float64

So, the label index is used with indexing and the positional one with slicing. Now let’s use just the label indices in both indexing and slicing. We need the loc attribute to this end:

In [21]:
# indexing with loc
nums.loc[3]
Out[21]:
0.87
In [22]:
# slicing with loc
nums.loc[2:4]
Out[22]:
2    4.11
3    0.87
4    2.76
dtype: float64

As you can see, now the label index is used in both cases. And now let’s use just the positional indices, just like in a regular Python list:

In [23]:
# indexing with iloc
nums.iloc[3]
Out[23]:
2.76
In [24]:
# slicing with iloc
nums.iloc[2:4]
Out[24]:
3    0.87
4    2.76
dtype: float64

The indexers are also used with DataFrames, which we are going to see in the next part of the series.

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