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

creating empty dataframe and adding values thru loop

i created an empty dataframe with 2 columns

df = pd.DataFrame(columns = ['pred', 'sim'])

and i want to add values to the DF using a loop i tried :

for i in range(5):
   df['pred'][i]=i
   df['sim'][i]=i

but i’m getting error : 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 :

That’s because the length of the dataframe is 0, and therefore every index is out of bounds. You have to append at the end of the dataframe:

for i in range(5):
    df = df.append(dict(pred=i,sim=i),ignore_index=True)
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