【问题标题】:How to loop through the dataframe after droping some rows from the dataframe?从数据框中删除一些行后如何遍历数据框?
【发布时间】:2019-04-17 19:59:03
【问题描述】:

我有一个数据框,我从中删除了一些行。但是我一直面临的问题是,当我尝试根据索引值循环数据帧时,它给了我“关键错误!”由于数据框中缺少某些索引。如何循环遍历数据框?

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()
  • for i in range(len(df)):... df.iloc[i]
  • @QuangHoang 如果可以避免,尽量不要像for i in range(len(df)): …df.iloc[i] 那样迭代,它非常慢,see here 解释原因。
  • @Asmus 同意了。但这不是我要说的。我只是想纠正 op 的代码。
  • stackoverflow.com/a/41022840/8363478这可能对你有帮助

标签: python pandas loops sentiment-analysis


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2019-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 2020-11-11
    • 2017-03-26
    • 2019-12-15
    相关资源
    最近更新 更多