Home > Mobile >  creating empty dataframe and adding values thru loop
creating empty dataframe and adding values thru loop

Time:11-26

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

CodePudding user response:

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)
  • Related