IndexError: single positional indexer is out-of-bounds for my Pandas Dataframe


IndexError: single positional indexer is out-of-bounds for my Pandas Dataframe



I am trying to perform certain addition operation to the columns in my dataframe and then store the values within the same dataframe. My DataFrame has 3796 rows and 11 columns containing data from the year 1928 till 2000, with weekly data. I want to take a weighted average of the data points(depending upon their dates) and store the values in the same dataframe. My code is as follows:


import pandas as pd
import numpy as np
df=pd.read_excel("D:SUMMER INTERNSHIP CONCORDIAUpdated Work
hereKitsim Reservoir (Number 220)Book3_Final.xlsx")
a=0
b=0
k=20
p=3
for z in range(3,11):
a=0
b=0
for x in range(1928,2000):
for y in range(0,12):
if df.iloc[a,1]==1:
df.iloc[b,k]=(7*df.iloc[a,z]+7*df.iloc[a+1,z]+7*df.iloc[a+2,z]+7*df.iloc[a+3,z]+3*df.iloc[a+4,z])/31
a=a+4
b=b+1
if df.iloc[a,1]==2:
df.iloc[b,k]=(4*df.iloc[a,z]+7*df.iloc[a+1,z]+7*df.iloc[a+2,z]+7*df.iloc[a+3,z]+3*df.iloc[a+4,z])/28
a=a+4
b=b+1
if df.iloc[a,1]==4:
df.iloc[b,k]=(1*df.iloc[a,z]+7*df.iloc[a+1,z]+7*df.iloc[a+2,z]+7*df.iloc[a+3,z]+7*df.iloc[a+4,z]+6*df.iloc[a+5,z])/30
a=a+5
b=b+1
if df.iloc[a,1]==3:
df.iloc[b,k]=(4*df.iloc[a,z]+7*df.iloc[a+1,z]+7*df.iloc[a+2,z]+7*df.iloc[a+3,z]+6*df.iloc[a+4,z])/31
a=a+4
b=b+1
if df.iloc[a,1]==5:
df.iloc[b,k]=(3*df.iloc[a,z]+7*df.iloc[a+1,z]+7*df.iloc[a+2,z]+7*df.iloc[a+3,z]+6*df.iloc[a+4,z])/31
a=a+4
b=b+1
if df.iloc[a,1]==6:
df.iloc[b,k]=(3*df.iloc[a,z]+7*df.iloc[a+1,z]+7*df.iloc[a+2,z]+7*df.iloc[a+3,z]+6*df.iloc[a+4,z])/30
a=a+4
b=b+1
if df.iloc[a,1]==7:
df.iloc[b,k]=(1*df.iloc[a,z]+7*df.iloc[a+1,z]+7*df.iloc[a+2,z]+7*df.iloc[a+3,z]+7*df.iloc[a+4,z]+2*df.iloc[a+5,z])/31
a=a+5
b=b+1
if df.iloc[a,1]==8:
df.iloc[b,k]=(5*df.iloc[a,z]+7*df.iloc[a+1,z]+7*df.iloc[a+2,z]+7*df.iloc[a+3,z]+5*df.iloc[a+4,z])/31
a=a+4
b=b+1
if df.iloc[a,1]==9:
df.iloc[b,k]=(2*df.iloc[a,z]+7*df.iloc[a+1,z]+7*df.iloc[a+2,z]+7*df.iloc[a+3,z]+7*df.iloc[a+4,z])/30
a=a+5
b=b+1
if df.iloc[a,1]==10:
df.iloc[b,k]=(7*df.iloc[a,z]+7*df.iloc[a+1,z]+7*df.iloc[a+2,z]+7*df.iloc[a+3,z]+3*df.iloc[a+4,z])/31
a=a+4
b=b+1
if df.iloc[a,1]==11:
df.iloc[b,k]=(4*df.iloc[a,z]+7*df.iloc[a+1,z]+7*df.iloc[a+2,z]+7*df.iloc[a+3,z]+5*df.iloc[a+4,z])/30
a=a+4
b=b+1
if df.iloc[a,1]==12:
df.iloc[b,k]=(2*df.iloc[a,z]+7*df.iloc[a+1,z]+7*df.iloc[a+2,z]+7*df.iloc[a+3,z]+3*df.iloc[a+4,z])/31
a=a+5
b=b+1
k=k+1
p=p+1



The error displayed was:


D:anacondalibsite-packagespandascoreindexing.py in
_is_valid_integer(self, key, axis)
1540 l = len(ax)
1541 if key >= l or key < -l:
-> 1542 raise IndexError("single positional indexer is out-of-
bounds")
1543 return True
1544

IndexError: single positional indexer is out-of-bounds



I read an answer which stated that it might be because I do not have enough columns for my value to get stored there, for the same, I created 50 empty columns in the Dataframe, but still the error was the same. I would also like to mention that when I am trying ,


a=0
b=0
k=20
z=3
df.iloc[b,k]=(7*df.iloc[a,z]+7*df.iloc[a+1,z]+7*df.iloc[a+2,z]+7*df.iloc[a+3,z]+3*df.iloc[a+4,z])/31
a=a+4
b=b+1



in a separate cell (without loop), it works totally fine. How shall I proceed now !?





can you add some sample row data?
– Dickster
Jun 30 at 0:48









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift