简体   繁体   English

从数据框中删除一些行后如何遍历数据框?

[英]How to loop through the dataframe after droping some rows from the dataframe?

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

您必须在过滤后重置索引:

dataset = dataset[dataset['reviews.rating']!=3].reset_index()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM