Select Page

How to Solve Python IndexError: single positional indexer is out-of-bounds

by | Data Science, Programming, Python, Tips

Indexing is an essential tool for storing and handling large and complex datasets with rows and columns. In Python, we use index values within square brackets to perform the indexing. If we try to access an index beyond the dimensions of the dataset, we will raise the error: IndexError: single positional indexer is out-of-bounds.

This tutorial will go through the error in detail, and we will go through an example scenario to learn how to solve the error.


IndexError: single positional indexer is out-of-bounds

What is an IndexError?

Python’s IndexError occurs when the index specified does not lie in the range of indices in the bounds of an array. In Python, index numbers start from 0. Let’s look at an example of a typical Python array:

animals = ["lion", "sheep", "whale"]

This array contains three values, and the first element, lion, has an index value of 0. The second element, sheep, has an index value of 1. The third element, whale, has an index value of 2.

If we try to access an item at index position 3, we will raise an IndexError.

print(animals[3])
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
1 print(animals[3])

IndexError: list index out of range

What is a DataFrame?

A DataFrame is a data structure that organizes data into a 2-dimensional table of rows and columns. The Python module Pandas works with DataFrames.

What is iloc()?

Pandas offers large-scale data analysis functions like the iloc() function, which enables us to select particular rows, columns, or individual cells of a dataset. The iloc() function performs integer-based indexing for selection by position. iloc() will raise “IndexError: single positional indexer is out-of-bounds” if a requested index is out-of-bounds. However, this error will not occur if you use a slice index, for example,

array[:slice_index]

Slice indexing allows for out-of-bounds indexing, which conforms with Python/numpy slice semantics. Let’s look at an example of the IndexError.

Example : Accessing a Column That Does Not Exist

Let’s create a DataFrame and attempt to access a particular column in the DataFrame. The dataset will contain a list of five car owners and will store each car owner’s city of residence and the brand of car they own. First, we must import Pandas and then define the rows that comprise our DataFrame. One row will store names, one will store cities, and one will store cars.

import pandas as pd
df = pd.DataFrame({'Name': ['Jim', 'Lisa', 'Paul', 'Carol', 'Biff'],

                    'City': ['Lisbon', 'Palermo', 'Sofia', 'Munich', 'Bangkok'],

                    'Car': ['Mercedes', 'Bentley', 'Ferrari', 'Rolls Royce', 'Aston Martin']})


if we print the DataFrame to the console, we will get the following arrangement of data in three rows and five columns.

print(df)
  Name     City           Car
0    Jim   Lisbon      Mercedes
1   Lisa  Palermo       Bentley
2   Paul    Sofia       Ferrari
3  Carol   Munich   Rolls Royce
4   Biff  Bangkok  Aston Martin

Let’s try to access the fifth column of the dataset using iloc(). In this example, it looks like:

print(df.iloc[:,5])
IndexError: single positional indexer is out-of-bounds

We raise the IndexError because we tried to access the fifth column of the dataset, and the fifth column does not exist for this particular dataset.

Solution

To solve this error, we can start by getting the shape of the dataset:

print(df.shape)
(5, 3)

This result tells us that the dataset has five rows and three columns, which means we can only use column index up to 2. Let’s try to take the car column with index 2.

print(df.iloc[:,2])
0        Mercedes
1         Bentley
2         Ferrari
3     Rolls Royce
4    Aston Martin
Name: Car, dtype: object

The code runs, and we can extract the car column from the dataset and print it to the console.

We can also access one particular value in the dataset by using two separate pairs of square brackets, one for the row and one for the column. Let’s try to get the car that Jim from Lisbon owns:

# Get particular value in row

jim_car = df.iloc[0][2]

print(jim_car)
Mercedes

The code runs and prints the value specific to row 0 column 2.

We can take a dataset slice using a colon followed by a comma then the slice. Let’s look at an example of slicing the first two columns of the car dataset:

print(df.iloc[:, 0:2])
  Name     City
0    Jim   Lisbon
1   Lisa  Palermo
2   Paul    Sofia
3  Carol   Munich
4   Biff  Bangko

We can also use slice indices out of the bound of the dataset; let’s use slicing to get five columns of the dataset

print(df.iloc[:, 0:5])
    Name     City           Car
0    Jim   Lisbon      Mercedes
1   Lisa  Palermo       Bentley
2   Paul    Sofia       Ferrari
3  Carol   Munich   Rolls Royce
4   Biff  Bangkok  Aston Martin

Although the dataset only has three columns, we can use slice indexing for five because slice indexers allow out-of-bounds indexing. Therefore we will not raise the IndexError: single positional indexer is out-of-bounds. Go to the article titled: “How to Get a Substring From a String in Python“.

Summary

Congratulations on reading to the end of this tutorial! The error “Indexerror: single positional indexer is out-of-bounds” occurs when you try to access a row/column with an index value out of the bounds of the pandas DataFrame. To solve this error, you must use index values within the dimensions of the dataset. You can get the dimensionality of a dataset using shape. Once you know the correct index values, you can get specific values using the iloc() function, which does integer-location based indexing.

It is important to note that using a slice with integers in the iloc() function will not raise the IndexError because slice indexers allow out-of-bounds indexing.

For further reading on Python IndexError, go to the articles:

To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.