Reputation: 383
A problem in this part of my code causes KeyError: -1
Do any of you know what might cause this?
for i in range(len(B130317)):
if B130317['LON'][i] != B130317['LON'][i-1]:
currentID += 1
newID.append(currentID)
Upvotes: 0
Views: 120
Reputation: 9494
If B130317['LON']
is an empty list, B130317['LON'][i-1]
for i=0
will throw KeyError: -1
exception.
I don't know what is your business logic but maybe you should consider changing the loop to be:
for i in range(len(B130317['LON'])):
# your logic
Upvotes: 1
Reputation: 126
based on the comments of @Badgy:
for i in range(1,len(B130317)):
if B130317['LON'][i] != B130317['LON'][i-1]:
currentID += 1
newID.append(currentID)
or:
for i in range(len(B130317)-1):
if B130317['LON'][i] != B130317['LON'][i+1]:
currentID += 1
newID.append(currentID)
Upvotes: 1