1

I have a dataframe from which i have deleted some rows. But the problem i have been facing is when i try to loop through dataframe on the basis of index values it gives me 'key error!' due to some indices are missing from the dataframe. How to loop through the dataframe?

dataset =pd.read_csv('sentimentAnalysis.csv') # dataset imported
dataset = dataset[dataset['reviews.rating']!=3] #dropped the rows which 
                                                 contain ratings =3
for i in range[0,5000]:    #encounter error at i = 222 cause that row is missing due to the previous line of code
    #XXXXXXXXXXXXXXXXXXX
petezurich
  • 9,280
  • 9
  • 43
  • 57
  • 4
    You have to reset your index after filtering: `dataset = dataset[dataset['reviews.rating']!=3].reset_index()` – Erfan Apr 17 '19 at 20:04
  • `for i in range(len(df)):... df.iloc[i]` – Quang Hoang Apr 17 '19 at 20:24
  • @QuangHoang If you can avoid it, try not to iterate like `for i in range(len(df)): …df.iloc[i]`, its terribly slow, [see here](https://engineering.upside.com/a-beginners-guide-to-optimizing-pandas-code-for-speed-c09ef2c6a4d6) for an explanation why. – Asmus Apr 17 '19 at 21:27
  • @Asmus agreed. But that’s not me to say. I was just trying to correct op’s code. – Quang Hoang Apr 17 '19 at 21:42
  • https://stackoverflow.com/a/41022840/8363478 this may help you – impopularGuy Apr 18 '19 at 01:56
  • @Erfan Should that comment be an answer? Seems like you could already have got 4 upvotes :-) – Darren Cook Apr 18 '19 at 10:49
  • Added it as answer. Thanks for the heads up @DarrenCook :). Probably wont get the upvotes anymore though. – Erfan Apr 18 '19 at 11:16

1 Answers1

1

You have to reset your index after filtering:

dataset = dataset[dataset['reviews.rating']!=3].reset_index()
Erfan
  • 40,971
  • 8
  • 66
  • 78