0
_LIST=[]
J=1
for i,r in df.iterrows():
   _VAR=r["A"]
if (_VAR==_LIST[J-1]):

I just want to compare _VAR value to the value in list which I got in last iteration. but getting error on this line, "List Index Out Of Range"

Here "A" is a column in dataframe df.

7
  • you havent initialized the list with any elements and trying to get an element. that is why, "List Index Out Of Range". be more clear on what you want Commented Aug 31, 2017 at 5:49
  • but also if I am giving _LIST[0]='' AFTER _list=[] I am getting error on that line only Commented Aug 31, 2017 at 5:51
  • @gsamaras ofcourse. But I believe OP missed the code where he is initialising the list. Otherwise it is pretty simple to see why the out of index error is coming. Thats why I asked him to be more clearer. Commented Aug 31, 2017 at 5:53
  • This code looks terrible. Why do you name variables like this? What are you actually trying to do? You should just use _Var.isin(_LIST). Commented Aug 31, 2017 at 5:54
  • There are multiple issues with your code. 1) You list is empty 2) You have initialized "J" and then trying to use "j-1" 3) Your naming convention is not according to the PEP-8 or even generally accepted standards Commented Aug 31, 2017 at 5:55

2 Answers 2

2

LIST is empty (since you don't initialize it or populate in any way), thus the element you are indexing doesn't exist, which results in the error you witness.

Sign up to request clarification or add additional context in comments.

Comments

1

It looks like you are accessing the index for which a list element does not exist

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.