single positional indexer is out-of-bounds: an error


why dataframe.iloc[1,5] return an error but dataframe.iloc[[1,5]] not?

Asked By: roro eiad



dataframe.iloc[1,5] returns an error because it is trying to access a single scalar value at the intersection of row 1 and column 5 in the DataFrame.

On the other hand, dataframe.iloc[[1,5]] does not return an error because it is trying to access multiple rows (rows 1 and 5) in the DataFrame. In this case, the output is a new DataFrame containing those specific rows.

The .iloc attribute is used to access a specific location in the DataFrame by index position. When passing a single integer value to .iloc, it will treat it as the index of the row, and when passing a list of integers, it will treat it as the indices of the rows.

So in short, when you use dataframe.iloc[1,5] it’s trying to access a single value at a specific location, which doesn’t exist in the dataframe. But when using dataframe.iloc[[1,5]] you’re accessing the rows with those indexes, which exist in the dataframe.

Answered By: Lahcen YAMOUN

Without looking at dataframe, no one can tell what you are dealing with. Only help you can get without sharing dataframe is documentation to get idea on how iloc actually works.

Answered By: Shubham Patel
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.