Home > Net >  I'm getting this IndexError: single positional indexer is out-of-bounds for the below code
I'm getting this IndexError: single positional indexer is out-of-bounds for the below code

Time:05-26

Dataset has 50 rows and 75 columns. I'm getting this single positional indexer is out-of-bounds error. Can anyone please help me with this?

def load_real_samples():
    # load cifar10 dataset
    #(trainX, _), (_, _) = load_data()
    dataset = []
    #print(trainX.shape)
    paths = pd.read_csv(r"C:\Users\Vaishnav\Documents\MiniProject\data_Mild.csv")
    paths.head(5)
    for i in range(1,50):
        if(os.path.exists(paths.iloc[i][i])):
            im = Image.open(paths.iloc[i][i])
            resized_im = im.resize((round(im.size[0]*0.0625), round(im.size[1]*0.0625)))
            image=asarray(resized_im)
            data = asarray(image)
            dataset.append(data)
    trainX=np.array(dataset)
    #y_train=np.array([0,0,0,0])
    print(trainX.shape)
    X = trainX.astype('float32')
    X = (X - 127.5) / 127.5
    return X
dataset = load_real_samples()

Error:

paths.head(5)
      9     for i in range(1,50):
---> 10         if(os.path.exists(paths.iloc[i][i])):
     11             im = Image.open(paths.iloc[i][i])
     12             resized_im = im.resize((round(im.size[0]*0.0625), round(im.size[1]*0.0625)))

IndexError: single positional indexer is out-of-bounds

CodePudding user response:

Index out-of-bounds means that there are less than 50 indices in the paths data frame. Moreover, when you use iloc[i][i], it means you access i-row and i-element within the first_row, and if it is not a list in the cell but a string you will get a key error. Try following:

for i in range(1, paths.shape[0]):
        if(os.path.exists(paths.iloc[i].values[0])):
            im = Image.open(paths.iloc[i].values[0])

Or try to access you column with the paths to avoid using range():

for path in paths.your_column_with_path_strings:
         if(os.path.exists(path)):
            im = Image.open(path)
  • Related