Fixing Python iloc IndexError: single positional indexer is out-of-bounds

Python iloc IndexError: single positional indexer is out-of-bounds is an error which occurs when you try to index a column or a row which has a number outside the dimensions of your dataframe.

In today’s article I am going to deal with a confusing error and explain why it takes place and how to fix it, with a set of possible solutions.

Exploring the Python iloc IndexError: single positional indexer is out-of-bounds

This is an error which occurs when you try to index a column or a row which has a number outside the dimensions of your dataframe.

You should avoid mixing between different errors. The error message should look like the error message bellow.

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

The method bellow will solve the error for most people, I hope it does for you.

The Method that solved my problem : 

Correctly index a Dataframe row or column

Before trying to solve this error we should understand why the error is happening in the first place.

Single positional indexer is out-of-bounds means that the column or row we are trying to index does not exist in the dataframe. For example the number of the row you are trying to index is 10 while you have less than 11 rows in your Dataset.

I think the root of the problem is clear by now. The solution is also very predictable, only try to index columns or rows which have numbers that are inferior to the dimensions of the dataframe.

An example is better than a thousand words, so let us create a hypothetical example, to explain this solution.

Assuming we have a dataframe, the dataframe has 4 rows and 4 columns.

                                                                       #
mydataframe = pd.DataFrame({'Brand': ['HP', 'MAC', 'ASUS', 'DELL'],
                            'ReleaseYear': ['2018', '2020', '2019', '2021'],
                            'Price': ['1200', '1250', '1340', '990'],
                            'InStock': ['111', '99', '65', '67']})
                                                                       #

We want to index column number 9, but there is no column number 9 which should be the 10th column in the dataframe.

                                                                       #
data = mydataframe.iloc[:,9].values
                                                                       #

We will get the error bellow, since the dataframe contains only 4 columns and 4 rows.

                                                                       #
single positional indexer is out-of-bounds
                                                                       #

Thank you for reading this blog post, I hope your error is gone now that you understand why it happened in the first place.

This should be enough to get rid of the error for good.

Summing-up : 

This is the end of our article, I hope the solutions I presented worked for you, Learning Python is a fun journey, do not let the errors discourage you. Keep coding and cheers.

Thank you for reading, keep coding and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/