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

5.2K    Asked by DrucillaTutt in Python , Asked on Apr 15, 2021

 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
runfile('C:/Users/name/Desktop/Machine Learning/Data preprocessing      template.py', wdir='C:/Users/taylorr2/Desktop/Machine Learning')
Traceback (most recent call last):
  File "", line 1, in
    runfile('C:/Users/name/Desktop/Machine Learning/Data preprocessing  template.py', wdir='C:/Users/taylorr2/Desktop/Machine Learning')
IndexError: single positional indexer is out-of-bounds
I read a question on here regarding the same error and have tried
import numpy as np
import pandas as pd
import matplotlib.pyplot as py
Dataset = pd.read_csv('filename.csv', sep = ',')
table = Dataset.find(id='AlerId')
rows = table.find_all('tr')[1:]
data = [[cell.text for cell in row.find_all('td')] for row in rows]
Dataset1 = pd.DataFrame(data=data, columns=columns)
X = Dataset1.iloc[:,:-1].values
Y = Dataset1.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 think this might have just confused me more and now am in even more of a state.
Any suggestions?

Answered by Drucilla Tutt

The code indexerror: single positional indexer is out-of-bounds, shows that you misunderstood iloc function. The value before the colon(:) is the index of rows and the after ‘:’ represents the index of columns.

As stated above by you, You have less than 19 features in your dataset. But you are passing the index value of the 19th feature. That’s why this error is there.

You should change the value according to the number of features in your datasets.

This code in which error is caused by:

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

Hope this answer helps.



Your Answer

Interviews

Parent Categories