Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

IndexError after trying to iter over rows and columns

Imagine a table with rows and columns. I want to read the table row by row. I don’t understand what has to get fixed and what’s the best way to do so:

import pandas as pd

num_rows = 4
num_cols = 5
value = "test"

for i in range(num_rows):
    s = pd.Series()
    for c in range(num_cols):
        s[c] = value

Output:

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File "c:\Users\chris\projects\stockfinder\venv\lib\site-packages\pandas\core\series.py", line 1067, in __setitem__
    values[key] = value
IndexError: index 0 is out of bounds for axis 0 with size 0

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use:

import pandas as pd
num_rows = 4
num_cols = 5
value = "test"

for i in range(num_rows):

    #here is the problem
    s = pd.Series(index=range(num_cols))

    for c in range(num_cols):
        s[c] = value
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading