pHorseSpec pHorseSpec - 7 months ago 11
Python Question

If Statements and Setting Variables in Python

I'm trying to pass in

df1
,
df2
,
df3
&
df4
sequentially into the
parseTwoPoleBreakers
function. However, only
df1
is being ran. Is there something wrong with my
if
elif
statements that causing
df2
,
df3
&
df4
to not be passed into the
parseTwoPoleBreakers
function?

i=0
for j in range(0,4):
if j==0:
df=df1
elif j==1:
df=df2
elif j==2:
df=df3
else:
df=df4
#execute this for each dataframe
while (i<7):
parseTwoPoleBreakers(7,8,"ab",i,df)
breakerid+=1
parseTwoPoleBreakers(9,10,"bc",i,df)
breakerid+=1
parseTwoPoleBreakers(11,12,"ca",i,df)
breakerid+=1
i+=1
#j+=1
print j

Answer

I'm trying to pass in df1, df2, df3 & df4 sequentially into the parseTwoPoleBreakers function

Your problem is most likely related to variable scoping inside the if statements or the fact the while i < 7 isn't entered after df1 because i == 7

Use a list instead for the df and did you mean to loop back over the 7 i values?

for df in [df1, df2, df3, df4]:
    #execute this for each dataframe
    for i in range(7):
        parseTwoPoleBreakers(7,8,"ab",i,df)
        breakerid+=1
        parseTwoPoleBreakers(9,10,"bc",i,df)
        breakerid+=1
        parseTwoPoleBreakers(11,12,"ca",i,df)
        breakerid+=1