简体   繁体   English

KeyError: 1 在处理上述异常的过程中,又发生了一个异常:

[英]KeyError: 1 During handling of the above exception, another exception occurred:

I have a dataframe which look like:我有一个 dataframe 看起来像:

df:
+---------------------------------------------------+-------------+------------+------------+
|Text_en                                            | pos_score   |  neg_score |  sent_score| 
+---------------------------------------------------+-------------+------------+------------+
|  inspir afternoon bang stori ahmad sahroni v AS...|  0.000      |  0.0       |  0         |    
|                                                   |  0.000      |  0.0       |  0         |      
|  some drastic measur taken manag bodi temperatu.  |  1.625      |  0.5       |  1         |     
|  ahmad sahroni tu                                 |  0.000      |  0.0       |  0         |    
|  busi success mudah legisl mandat who make inte...|  1.125      |  0.0       |  1         |   
+---------------------------------------------------+-------------+------------+------------+ 

I want to generate/assigned positive text, negative text, neutral text for further processing using this code:我想使用以下代码生成/分配正面文本、负面文本、中性文本以进行进一步处理:

pos_text=""
neg_text=""
neut_text=""

for i in range(len(df_copy.index)):
    if(df_copy.loc[i]["sent_score"]==1):
        pos_text+=df_copy.loc[i]["Text_en"]
    elif(df_copy.loc[i]["sent_score"]==-1):
        neg_text+=df_copy.loc[i]["Text_en"]
    else:
        neut_text+=df_copy.loc[i]["Text_en"]

list_text = [pos_text,neg_text,neut_text]

But it raised an error:但它引发了一个错误:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2645             try:
-> 2646                 return self._engine.get_loc(key)
   2647             except KeyError:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 1

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-11-8ff17d5161f0> in <module>
      4 
      5 for i in range(len(df_copy.index)):
----> 6     if(df_copy.loc[i]["sent_score"]==1):
      7         pos_text+=df_copy.loc[i]["Text"]
      8     elif(df_copy.loc[i]["sent_score"]==-1):

~\anaconda3\lib\site-packages\pandas\core\indexing.py in __getitem__(self, key)
   1765 
   1766             maybe_callable = com.apply_if_callable(key, self.obj)
-> 1767             return self._getitem_axis(maybe_callable, axis=axis)
   1768 
   1769     def _is_scalar_access(self, key: Tuple):

~\anaconda3\lib\site-packages\pandas\core\indexing.py in _getitem_axis(self, key, axis)
   1962         # fall thru to straight lookup
   1963         self._validate_key(key, axis)
-> 1964         return self._get_label(key, axis=axis)
   1965 
   1966 

~\anaconda3\lib\site-packages\pandas\core\indexing.py in _get_label(self, label, axis)
    622             raise IndexingError("no slices here, handle elsewhere")
    623 
--> 624         return self.obj._xs(label, axis=axis)
    625 
    626     def _get_loc(self, key: int, axis: int):

~\anaconda3\lib\site-packages\pandas\core\generic.py in xs(self, key, axis, level, drop_level)
   3535             loc, new_index = self.index.get_loc_level(key, drop_level=drop_level)
   3536         else:
-> 3537             loc = self.index.get_loc(key)
   3538 
   3539             if isinstance(loc, np.ndarray):

~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2646                 return self._engine.get_loc(key)
   2647             except KeyError:
-> 2648                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2649         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2650         if indexer.ndim > 1 or indexer.size > 1:

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()

KeyError: 1

Anyway, at first, I run my code it worked like charm then I did some cleaning like dropping some duplicates rows then when I tried to run again I got those error无论如何,起初,我运行我的代码,它就像魅力一样,然后我做了一些清理,比如删除一些重复的行,然后当我再次尝试运行时,我得到了这些错误

The application of df.loc[i] expects "i" to be a valid index value in your pandas DataFrame. df.loc[i] 的应用程序期望“i”是 pandas DataFrame 中的有效索引值。 If you have dropped some rows (eg, duplicates), you have removed some of the indices and thus they are not in your DataFrame index anymore.如果您删除了一些行(例如,重复),您已经删除了一些索引,因此它们不再在您的 DataFrame 索引中。

Apply df.reset_index(drop=True, inplace=True) to generate a fresh index with consecutive numbers in your clean DataFrame.应用 df.reset_index(drop=True, inplace=True) 在干净的 DataFrame 中生成一个带有连续数字的新索引。

You generally don't want to loop over a dataframe in this way.您通常不想以这种方式循环 dataframe。 Instead, use loc to query and then do something with the results.相反,使用 loc 进行查询,然后对结果执行一些操作。

pos_text = df_copy.loc[df['sent_score'] == 1, 'Text_en']
neg_text = df_copy.loc[df['sent_score'] == -1, 'Text_en']
neut_text = df_copy.loc[df['sent_score'] == 0, 'Text_en'] 

its that [i] part for sure, because that's where it breaks.它是 [i] 的一部分,因为那是它中断的地方。 df_copy.loc[1] is an error there is no [1]. df_copy.loc[1] 是一个错误,没有 [1]。 So I need to reset my index using所以我需要使用重置我的索引

df_copy=df_copy.reset_index(drop=True)

It worked like a charm它就像一个魅力

暂无
暂无

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

相关问题 KeyError:在处理上述异常的过程中,发生了另一个异常 - KeyError: During handling of the above exception, another exception occurred 在处理上述异常的过程中,又发生了一个异常 - During handling of the above exception, another exception occurred KeyError: 'Tipos' 在处理上述异常期间,发生了另一个异常:Traceback(最近一次调用最后一次): - KeyError: 'Tipos' During handling of the above exception, another exception occurred: Traceback (most recent call last): 在处理上述异常期间,发生了另一个异常:Python程序 - During handling of the above exception, another exception occurred: Python program 在处理上述异常的过程中,又发生了一个异常:&amp; google sheet not found - During handling of the above exception, another exception occurred: & google sheets not found “在处理上述异常过程中,发生了另一个异常”for循环中的第一个输入 - “ during the handling of above exception, another exception occurred ” for the first input in the for loop 如何解决“在处理上述异常期间,发生了另一个异常:” - How to fix "During handling of the above exception, another exception occurred: " Flask-restful - 在处理上述异常的过程中,发生了另一个异常 - Flask-restful - During handling of the above exception, another exception occurred 关键错误:1 在处理上述异常的过程中,发生了另一个异常 - Key Error: 1 During handling of the above exception, another exception occurred 在处理上述异常的过程中,发生了另一个异常:'Date' - During handling of the above exception, another exception occurred: 'Date'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM