Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. Python
  3. Question
Python

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

Asked by Drucilla Tutt Apr 15, 2021 12.7K views 2 answers
Share

About this question

 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?

Your answer

2 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on May 7, 2024

The "IndexError: Single positional indexer is out-of-bounds" error typically occurs when using the iloc method in Python's pandas library to access elements of a DataFrame using an index that is out of range. Here are some common reasons why you might encounter this error and how to resolve it:


Invalid Index Value: Ensure that the index value you're using with iloc is within the valid range of the DataFrame's index. Remember that Python uses zero-based indexing, so valid index values range from 0 to len(df) - 1, where df is your DataFrame.

Empty DataFrame: If your DataFrame is empty (i.e., it has zero rows), attempting to access any element using iloc will result in this error. Make sure your DataFrame contains data before using iloc.

Incorrect Column or Row Index: Double-check that you're specifying the correct index for either the rows or columns depending on how you're using iloc. For example, if you're accessing a row, ensure the index value corresponds to the row number, and if you're accessing a column, ensure the index value corresponds to the column number.

Multi-dimensional Indexing: If you're trying to access elements of a DataFrame with multi-dimensional indexing using iloc, ensure that the index values provided are within the bounds of both dimensions.

Indexing beyond DataFrame Shape: Avoid attempting to access elements beyond the shape of the DataFrame. For example, if you have a DataFrame with dimensions n rows by m columns, attempting to access element (n+1, m) or (n, m+1) using iloc will result in this error.

Here's an example of how you might use iloc correctly to access DataFrame elements:

import pandas as pd
# Create a sample DataFrame
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
# Accessing a specific element by row and column index
element = df.iloc[0, 1]  # Accesses the element in the first row, second column
print(element)

Ensure that you're following these guidelines when using iloc to access elements in your DataFrame, and verify that the index values you're providing are valid for the size and shape of your DataFrame.

Was this helpful?

More Python discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest Python Blogs

Guides, tips and career advice on Python from JanBask experts.