Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iloc giving 'IndexError: single positional indexer is out-of-bounds'

Tags:

python

I am trying to encode some information to read into a Machine Learning model using the following

import numpy as np import pandas as pd import matplotlib.pyplot as py  Dataset = pd.read_csv('filename.csv', sep = ',')  X = Dataset.iloc[:,:-1].values Y = Dataset.iloc[:,18].values  from sklearn.preprocessing import LabelEncoder, OneHotEncoder labelencoder_X = LabelEncoder() X[:, 0] = labelencoder_X.fit_transform(X[:, 0]) onehotencoder = OneHotEncoder(categorical_features = [0]) X = onehotencoder.fit_transform(X).toarray() 

however I am getting an error that reads

IndexError: single positional indexer is out-of-bounds 
like image 588
Taylrl Avatar asked Mar 11 '17 19:03

Taylrl


People also ask

What is single positional indexer out of bounds?

What is this “Indexerror: single positional indexer is out-of-bounds” error? This is an index-based error that pops up when programmers try to access or call or use any memory that is beyond the scope of the index. Let suppose, you have a list that has five elements. This means, your index will start from 0 up till 4.

What is ILOC function in Python?

The iloc() function in python is defined in the Pandas module that helps us to select a specific row or column from the data set. Using the iloc method in python, we can easily retrieve any particular value from a row or column by using index values.

How do you reference a DataFrame index?

To get the index of a Pandas DataFrame, call DataFrame. index property. The DataFrame. index property returns an Index object representing the index of this DataFrame.


2 Answers

This error is caused by:

Y = Dataset.iloc[:,18].values 

Indexing is out of bounds here most probably because there are less than 19 columns in your Dataset, so column 18 does not exist. The following code you provided doesn't use Y at all, so you can just comment out this line for now.

like image 173
slonopotam Avatar answered Nov 03 '22 09:11

slonopotam


This happens when you index a row/column with a number that is larger than the dimensions of your dataframe. For instance, getting the eleventh column when you have only three.

import pandas as pd  df = pd.DataFrame({'Name': ['Mark', 'Laura', 'Adam', 'Roger', 'Anna'],                    'City': ['Lisbon', 'Montreal', 'Lisbon', 'Berlin', 'Glasgow'],                    'Car': ['Tesla', 'Audi', 'Porsche', 'Ford', 'Honda']}) 

You have 5 rows and three columns:

    Name      City      Car 0   Mark    Lisbon    Tesla 1  Laura  Montreal     Audi 2   Adam    Lisbon  Porsche 3  Roger    Berlin     Ford 4   Anna   Glasgow    Honda 

Let's try to index the eleventh column (it doesn't exist):

df.iloc[:, 10] # there is obviously no 11th column 

IndexError: single positional indexer is out-of-bounds

If you are a beginner with Python, remember that df.iloc[:, 10] would refer to the eleventh column.

like image 35
Nicolas Gervais Avatar answered Nov 03 '22 11:11

Nicolas Gervais