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

后端 未结 3 995
清酒与你
清酒与你 2020-12-03 00:20

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


        
相关标签:
3条回答
  • 2020-12-03 00:53

    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.

    0 讨论(0)
  • 2020-12-03 01:01

    Lenghten your CSV file: add more rows and columns. It was not working for me when file was small. I lenghtened and now it worked.

    0 讨论(0)
  • 2020-12-03 01:04

    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.

    0 讨论(0)
提交回复
热议问题