Skip to content
  • YouTube
  • FaceBook
  • Twitter
  • Instagram

Data Analytics Ireland

Data Analytics and Video Tutorials

  • Home
  • Contact
  • About Us
    • Latest
    • Write for us
    • Learn more information about our website
  • Useful Links
  • Glossary
  • All Categories
  • Faq
  • Livestream
  • Toggle search form
  • planning your machine learning model machine learning
  • How to use zip() function in Python Python
  • how to build a machine learning model machine learning
  • What are Lambda functions in Python? Python
  • YouTube channel lists – Python Data Cleansing Python Data Cleansing
  • How do I fix TypeError: unhashable type: ‘list’ Error? Articles
  • What is Jupyter notebook? Jupyter Notebook
  • What does the … operator do in Julia? julia

IndexError: single positional indexer is out-of-bounds

Posted on April 4, 2021October 10, 2021 By admin

Often you will get an error IndexError: single positional indexer is out-of-bounds that is referencing a row 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 were 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 let’s 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.

Index Error Tags:IndexError, out of bounds, positional indexer, Python

Post navigation

Previous Post: IndexError: list index out of range
Next Post: IndexError: index 2 is out of bounds for axis 0 with size 2

Related Posts

  • IndexError: index 2 is out of bounds for axis 0 with size 2 Index Error
  • IndexError: list index out of range Index Error

Select your language!

  • हिंदी
  • Español
  • Português
  • Français
  • Italiano
  • Deutsch
  • ValueError: cannot convert float NaN to integer Null values
  • how to select all records with SQL SQL
  • What is Visual Studio Code? Visual Studio
  • Tkinter python tutorial Python
  • What are measures in Tableau? data visualisation
  • How to sort a Python Dictionary Python
  • ValueError: pattern contains no capture groups Value Error
  • How to create a calculated field in Tableau data visualisation

Copyright © 2023 Data Analytics Ireland.

Powered by PressBook Premium theme

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT