我有一个数据框,其中删除了一些行。但是我一直面临的问题是,当我尝试根据索引值遍历数据帧时,它给了我“关键错误!”。由于某些索引从数据框中丢失。如何遍历数据框?
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
答案 0 :(得分:1)
您必须在过滤后重置索引:
dataset = dataset[dataset['reviews.rating']!=3].reset_index()