4

I have dataframe as given below:

df = 
          0
1      0.993995
2      1.111068
3      1.760940
.
.
.
49    40.253574
50    40.664486
51    41.083962

I am iterating through each row and print each element. My code is given below:

for idx,row in df.iterrows():
    print(df[0].iloc[idx])

Present output:

1.111068
1.76094
2.691832
.
.
40.664486
41.083962
Traceback (most recent call last):

  File "<ipython-input-46-80539a9081e5>", line 2, in <module>
    print(darkdf[0].iloc[idx])

  File "C:\Users\MM\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 1500, in __getitem__
    return self._getitem_axis(maybe_callable, axis=axis)

  File "C:\Users\MM\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 2230, in _getitem_axis
    self._validate_integer(key, axis)

  File "C:\Users\MM\Anaconda3\lib\site-packages\pandas\core\indexing.py", line 2139, in _validate_integer
    raise IndexError("single positional indexer is out-of-bounds")

IndexError: single positional indexer is out-of-bounds

Why this simple function is giving error. Someone could help me to understand what the error is saying?

Mainland
  • 4,110
  • 3
  • 25
  • 56
  • This link from a previous stackoverflow question might be helpful! [https://stackoverflow.com/questions/16476924/how-to-iterate-over-rows-in-a-dataframe-in-pandas](https://stackoverflow.com/questions/16476924/how-to-iterate-over-rows-in-a-dataframe-in-pandas) – Emily Brantner Oct 26 '19 at 15:31
  • I am actually doing row wise operation. Here I showed simple print function to explain the problem I am facing. Print is not my actual objective. This link talks about Print. How to iterate through rows – Mainland Oct 26 '19 at 15:34
  • Mmm tricky, I tried it on my own and it is fine. Could you print(index, df[0].iloc[idx]) seems like the index is not consistent. – Rodrigo Loza Oct 26 '19 at 15:35
  • Does this answer your question? [iloc giving 'IndexError: single positional indexer is out-of-bounds'](https://stackoverflow.com/questions/42739327/iloc-giving-indexerror-single-positional-indexer-is-out-of-bounds) – Taylrl Jun 10 '20 at 17:59

2 Answers2

3

First correct way for select is use DataFrame.loc:

print (df)
          0
1  0.993995
2  1.111068
3  1.760940

for idx,row in df.iterrows():
    print(df.loc[idx, 0])
0.9939950000000001
1.111068
1.7609400000000002

Problem in your solution:

If use Series.iloc function it select by position, not by labels.

So you want select 4.th row by selecting:

df[0].iloc[3]

but there is not 4.th (python counts from 0, so for select 4.th row need 3) row so raised error.

If use:

df[0].loc[3]

it working like you expected, because selecting index 3 (not position 4 which not exist) and column 0, but better is use:

df.loc[idx, 0]

because evaluation order matters.

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • 1
    Hi, Happy to see you reply to my question. Could you please educate me why I am getting error for simple iloc function? – Mainland Oct 26 '19 at 15:38
  • 1
    Now your response educated me about iloc. Thanks a lot. – Mainland Oct 26 '19 at 15:40
  • 1
    I tried following example and understood the difference: ` df1 = pd.DataFrame({"A":[10,20,30]},index=[2,4,6]) for idx,row in df1.iterrows(): print(df1.loc[idx,"A"])` – Mainland Oct 26 '19 at 15:47
2

You may want to use loc instead of iloc. iloc uses the zero-based row number, not indices. Your code is passing the indices, which go over the range of the zero-based row numbers, hence out-of-bounds.

KJ N
  • 96
  • 5