IndexError: index 2 is out of bounds for axis 0 with size 2

In Python you may come across IndexError: index 2 is out of bounds for axis 0 with size 2 from time to time.

The error relates to the use of indexes and the no of columns you are referencing.

We discussed single positional indexer is out-of-bounds and in that blog post it was related to referencing indexes for rows that do not exist.

In this blog post the error relates to referencing index for columns that do not exist.

Lets walk through the code below and where the problem arises

So lets refresh ourselves with the data that is in the excel CSV file below:

import pandas as pd

dataset = pd.read_csv('import_file.csv', sep = ',')

df = pd.DataFrame(dataset, columns=['Name','Age'])

a = df.iloc[4][2] #===>this allows you to print a particular row or value in that row

print(df)
print(a)

Output:
Traceback (most recent call last):
  File "C:/Users/haugh/OneDrive/dataanalyticsireland/YOUTUBE/IndexError_index_2_is_out_of_bounds_for_axis_0_with_size_2/index_2_out_of_bounds_for_axis_0_with_size_2.py", line 7, in <module>
    a = df.iloc[4][2] #===>this allows you to print a particular row or value in that row
  File "C:\Users\haugh\anaconda3\lib\site-packages\pandas\core\series.py", line 879, in __getitem__
    return self._values[key]
IndexError: index 2 is out of bounds for axis 0 with size 2

As you can see the column index value can be either 0 or 1 , this is because they represent the index values of the columns starting at 0 and ending at 1.

The problem arises here in this line ===> a = df.iloc[4][2] . Essentially the value is looking to reference a column with index value 2, but as we saw above that index value does not exist.

As a result by replacing it with a column index value within the proper range ( in this case 0 or 1), the error will disappear and the code will work as expected.

import pandas as pd

dataset = pd.read_csv('import_file.csv', sep = ',')

df = pd.DataFrame(dataset, columns=['Name','Age'])

a = df.iloc[4][0] #===>this allows you to print a particular row or value in that row

print(df)
print(a)

Gives:
       Name  Age
0       Joe   21
1      John   22
2       Jim   23
3      Jane   24
4  Jennifer   25
Jennifer


OR

import pandas as pd

dataset = pd.read_csv('import_file.csv', sep = ',')

df = pd.DataFrame(dataset, columns=['Name','Age'])

a = df.iloc[4][1] #===>this allows you to print a particular row or value in that row

print(df)
print(a)

Gives:
      Name  Age
0       Joe   21
1      John   22
2       Jim   23
3      Jane   24
4  Jennifer   25
25

Process finished with exit code -1073741819 (0xC0000005)

Finally, in a = df.iloc[4][1] you can also change the value 4, which is the index for that row to either 0,1,2,3 and the code will work with no errors as it brings back the values that you expect.

So in summary:

(A) This error happens when you try to use a column index value does not exist as it is outside the index values for the columns that are in the data set.

(B) If this error does occur always check the expected index values for each column and compare against what you are trying to return.

IndexError: single positional indexer is out-of-bounds

Often you will get an error IndexError: single positional indexer is out-of-bounds that is referencing a row that that does not exist based on its index value.

When you want to look at a particular row in Python, there is a way that you can reference the row and then the values within it.

Lets break it down further to understand how the error occurs and why and how to fix it.

How the error occurs?

When we look at the below code, it throws out the error we are trying to fix.

Digging deeper lets look at the file we are importing, and the values contained within them. From the CSV file:

the above values are imported. If we where to create a matrix of its index values, it would be as follows:

As can be seen already, the index values range from zero to four in both row values and the column values are an index value of 1.

In the below code though we are trying to reference a row index value of five, but that does not exist, hence the error.

Note that using “iloc” allows you to return all the row values or a particular row and column value, we will demonstrate that in the next section.

import pandas as pd

dataset = pd.read_csv('import_file.csv', sep = ',')

df = pd.DataFrame(dataset, columns=['Name','Age'])

a = df.iloc[5] #===>this allows you to print a particular row or value in that row

print(df)

Error:
IndexError: single positional indexer is out-of-bounds

How to fix this error?

First off lets just return the whole row say of index value two based on the below matrix:

This should return Jim and 23 in the output

import pandas as pd

dataset = pd.read_csv('import_file.csv', sep = ',')

df = pd.DataFrame(dataset, columns=['Name','Age'])

a = df.iloc[2] #===>this allows you to print a particular row or value in that row

print(df)
print(a)

Output:

       Name  Age
0       Joe   21
1      John   22
2       Jim   23
3      Jane   24
4  Jennifer   25
Name    Jim
Age      23
Name: 2, dtype: object

Process finished with exit code -1073741819 (0xC0000005)

We could also return either a name or age value as well, as long as they are within the range of values. This is achieved as follows:

Lets return just Jennifer’s age of 25 as follows:

import pandas as pd

dataset = pd.read_csv('import_file.csv', sep = ',')

df = pd.DataFrame(dataset, columns=['Name','Age'])

a = df.iloc[4][1] #===>this allows you to print a particular row or value in that row

print(df)
print(a)

Output:
      Name  Age
0       Joe   21
1      John   22
2       Jim   23
3      Jane   24
4  Jennifer   25
25

Process finished with exit code -1073741819 (0xC0000005)

So in summary:

(A) When you are looking to retrieve particular values in a row, you need to make sure you have a valid range of index values.

(B) Using “iloc” is a handy way to retrieve any value you want, but make sure you reference the correct index values.

IndexError: list index out of range

Estimated reading time: 3 minutes

Are you working with lists and getting the error IndexError: list index out of range while using Python? There is a very simple explanation to this, and its fix is very easy.

First of all lets understand what is going on with the list.

Traceback (most recent call last):
  File "C:/Users/haugh/OneDrive/dataanalyticsireland/YOUTUBE/IndexError_list_index_out_of_range/INDEX_ERROR_LIST_INDEX_OUT_OF_RANGE.py", line 4, in <module>
    print(data[4])
IndexError: list index out of range

Lists and their index values

In the below list, we have outputted its values and index values.

data = ['a','b','c','d']
for (i,item) in enumerate(data, start=0): #===> Loops through list and applies index values starting at zero
    print(i,item)

Output:
0 a
1 b
2 c
3 d

Process finished with exit code 0

As can be seen the program returns the list values and their indexes. Note that the index starts at zero as we have set start=0.

Start=0 can be set to any value you like, as can be seen here:

data = ['a','b','c','d']
for (i,item) in enumerate(data, start=1): #===> Loops through list and applies index values starting at zero
    print(i,item)

Output:
1 a
2 b
3 c
4 d

Process finished with exit code 0

OR

data = ['a','b','c','d']
for (i,item) in enumerate(data, start=22): #===> Loops through list and applies index values starting at zero
    print(i,item)

Output:
22 a
23 b
24 c
25 d

Process finished with exit code 0

The purpose of the index value is to tell the program where to start its index from, if left empty it starts at zero.

Lists and the no of index values

In the above examples the index values all occur on four rows.

This is important as when you are looping through the rows, it will not go beyond the length of the rows.

So in this example the enumerate function specifically counts the no rows and stores the index values with each, and then loops through the list till it hits the last one, without error.

data = ['a','b','c','d']
for (i,item) in enumerate(data, start=0): #===> Loops through list and applies index values starting at zero
    print(i,item)

Output:
0 a
1 b
2 c
3 d

Process finished with exit code 0

How to fix the error IndexError: list index out of range

So the reason we get the below is that the line print(data[4]) is looking for the row with index value 4, but we know that from observation that does not exist.

To fix this we would change the value 4 in print(data[4]) to any of 0,1,2,3, as they are the index values associated with the list.

data = ['a','b','c','d']
for (i,item) in enumerate(data, start=0): #===> Loops through list and applies index values starting at zero
    print(i,item)
print(data[4])

Output:
Traceback (most recent call last):
  File "C:/Users/haugh/OneDrive/dataanalyticsireland/YOUTUBE/IndexError_list_index_out_of_range/INDEX_ERROR_LIST_INDEX_OUT_OF_RANGE.py", line 4, in <module>
    print(data[4])
IndexError: list index out of range
0 a
1 b
2 c
3 d

Applying a correct valid index value:
data = ['a','b','c','d']
for (i,item) in enumerate(data, start=0): #===> Loops through list and applies index values starting at zero
    print(i,item)
print(data[3])

Yields with no error:
0 a
1 b
2 c
3 d
d

So in summary when working with lists and their index values it is important:

(A) Understand the length of your list.

(B) Where your index values start and finish.

This error is easily fixable, but in your code you just need to make sure that you referencing values that are in the range of your index values.