John Doe
John Doe

Reputation: 1

Iterating in a For Loop to check if previous value is greater than current value

pitch_per_ab = []
pitch_of_pa = pitcher['PitchofPA']

for i in range(len(pitch_of_pa)):
    prev_elem = pitch_of_pa[i-1]
    current_elem = pitch_of_pa[i]
    if current_elem <= prev_elem:
       np.append(pitch_per_ab, prev_elem)
    
pitch_per_ab

This is my current code, when I run it i get a KeyError that simply says "-1": photo of error

Ideally, it would run and if the previous value in pitch_of_pa is greater than the current, than it will append the previous value to the list pitch_per_ab

Upvotes: -1

Views: 1245

Answers (1)

This is happens due to:

prev_elem = pitch_of_pa[i-1]

i starts in 0 and when this row is executed, the value -1 causes the error:

KeyError: -1

Upvotes: 0

Related Questions