. Advertisement .
..3..
. Advertisement .
..4..
For the “indexerror: single positional indexer is out-of-bounds” problem. I tried to fix it, but It doesn’t work and returns the result I want. Here is my program:
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()
and, the result returns the waning test as follow:
IndexError: single positional indexer is out-of-bounds
has occurred. I’ve checked the entire command line but still can’t find the mistake.
The cause: This error happened because of following reason:
In your Dataset, there are less than 19 columns, so indexing is out of bounds and there is no the 18th column. Y was not used in the code you provided, so now you can just comment this line.
Solution: You have to index a row/column with a number that is smaller than the dimensions of your dataframe, it must be in the bounds. In your example, you need to have more than 19 columns.
This occurs when you index a row/column using a number larger than your
dataframe
. This could be the case when you have three columns and want to get the eleventh.There are 5 rows and 3 columns.
Let’s index the 11th column (it doesn’t exist).
For beginners with Python,
df.iloc[:, 10]
refers to the eleventh column.