Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.(5)

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

ITtutoria

ITtutoria Logo ITtutoria Logo

ITtutoria Navigation

  • Python
  • Java
  • Reactjs
  • JavaScript
  • R
  • PySpark
  • MYSQL
  • Pandas
  • QA
  • C++
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA
Home/ Questions/Is It Okay To Fix the indexerror: single positional indexer is out-of-bounds problem?
Next
Answered
Tiago Charpentier
  • 21
Tiago Charpentier
Asked: May 12, 20222022-05-12T09:51:26+00:00 2022-05-12T09:51:26+00:00In: python

Is It Okay To Fix the indexerror: single positional indexer is out-of-bounds problem?

  • 21

. 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.

indexerror
  • 2 2 Answers
  • 67 Views
  • 0 Followers
  • 0
Answer
Share
  • Facebook
  • Report

2 Answers

  • Voted
  • Oldest
  • Recent
  • Random
  1. Best Answer
    lyytutoria Expert
    2022-06-02T09:29:10+00:00Added an answer on June 2, 2022 at 9:29 am

    The cause: This error happened because of following reason:

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

    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.

    • 16
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report
  2. Maël Granger
    2022-05-25T19:40:20+00:00Added an answer on May 25, 2022 at 7:40 pm

    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.

    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']})

    There are 5 rows and 3 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 index the 11th column (it doesn’t exist).

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

    IndexError: single positional indexer is out-of-bounds

    For beginners with Python, df.iloc[:, 10] refers to the eleventh column.

    • 7
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question
  • How to Split String by space in C++
  • How To Convert A Pandas DataFrame Column To A List
  • How to Replace Multiple Characters in A String in Python?
  • How To Remove Special Characters From String Python

Explore

  • Home
  • Tutorial

Footer

ITtutoria

ITtutoria

This website is user friendly and will facilitate transferring knowledge. It would be useful for a self-initiated learning process.

@ ITTutoria Co Ltd.

Tutorial

  • Home
  • Python
  • Science
  • Java
  • JavaScript
  • Reactjs
  • Nodejs
  • Tools
  • QA

Legal Stuff

  • About Us
  • Terms of Use
  • Privacy Policy
  • Contact Us

DMCA.com Protection Status

Help

  • Knowledge Base
  • Support

Follow

© 2022 Ittutoria. All Rights Reserved.

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.