I am trying to create an array of numbers (socarray) that are between 0 and 100. The array is initiated with a single item (integer 50).
The number to append is soc and is initiated at 50. The value of soc changes conditional on the value of a prediction column in a dataframe (df['Pred']).
- If df['Pred']>0 and soc < 100 then soc += 50 and is appended to socarray.
- If df['Pred'] <0 and soc > 0 then soc -=50 and is appended to socarray
- else we just append soc (without adding or substracting from the value).
My code is below. I am using For loop, a while loop and an If statements.
The range is set to iterate 10 times (the length of the dataframe). However, the output of the code is an array of length 82.
soc = 50
socarray = [50]
for i in range(1,10):
counter = 1
while counter < len(df):
if (df.iloc[i]['Pred'] > 0) & (soc<100):
soc += 50
socarray.append(soc)
counter += 1
elif (df.iloc[i]['Pred'] < 0) & (soc>0):
soc -= 50
socarray.append(soc)
counter += 1
else:
soc = soc
socarray.append(soc)
counter += 1
If I embed the array into the for loop it outputs an array length 10 but it doesn't follow the correct functionality (specifically it doesn't subtract 'soc -= 50' when it should do)
soc = 50
for i in range(1,len(df)):
socarray = [50]
counter = 1
while counter < len(df):
if (df.iloc[i]['Pred'] > 0) & (soc<100):
soc += 50
socarray.append(soc)
counter += 1
elif (df.iloc[i]['Pred MW'] < 0) & (soc>0):
soc -= 50
socarray.append(soc)
counter += 1
else:
soc = soc
socarray.append(soc)
counter += 1
Can someone help me?