首页 > 解决方案 > 尝试访问行索引时出现 Pandas KeyError

问题描述

我有一个名为 clean 的数据框,然后将其分成两个样本:train_data 和 test_data,代码如下:

train_data = clean.sample(frac=0.75)
test_data = clean.drop(train_data.index)

我正在尝试从 train_data 数据帧制作一个词频数据帧。我从代码开始

from collections import defaultdict as dct

phrases = []
for word in train_data['Message']:
    phrases.append(word.split())
    
ham = dct(int)
spam = dct(int)
    
for i in range(len(phrases)):
    if train_data['Category'][i] == 'ham':
        print(train_data['Category'][i])
    elif train_data['Category'][i] == 'spam':
        print(train_data['Category'][i])

if train_data['Category'][i] == 'ham':但是当索引 i 不在 train_data 中时,它给了我一个错误:

KeyError                                  Traceback (most recent call last)
~/Library/Python/3.8/lib/python/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3079             try:
-> 3080                 return self._engine.get_loc(casted_key)
   3081             except KeyError as err:

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: 5

The above exception was the direct cause of the following exception:

KeyError                                  Traceback (most recent call last)
<ipython-input-97-17de52f682b3> in <module>
      9 
     10 for i in range(len(phrases)):
---> 11     if train_data['Category'][i] == 'ham':
     12         print(train_data['Category'][i])
     13     elif train_data['Category'][i] == 'spam':

~/Library/Python/3.8/lib/python/site-packages/pandas/core/series.py in __getitem__(self, key)
    851 
    852         elif key_is_scalar:
--> 853             return self._get_value(key)
    854 
    855         if is_hashable(key):

~/Library/Python/3.8/lib/python/site-packages/pandas/core/series.py in _get_value(self, label, takeable)
    959 
    960         # Similar to Index.get_value, but we do not fall back to positional
--> 961         loc = self.index.get_loc(label)
    962         return self.index._get_values_for_loc(self, loc, label)
    963 

~/Library/Python/3.8/lib/python/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3080                 return self._engine.get_loc(casted_key)
   3081             except KeyError as err:
-> 3082                 raise KeyError(key) from err
   3083 
   3084         if tolerance is not None:

KeyError: 5

train_data 看起来像这样(前 20 行):

  Category                                            Message
1635      ham  you have come into my life and brought the sun...
3724      ham                    nothing splwat abt u and whr ru
1531      ham        oh dang i didnt mean o send that to you lol
1672     spam  urgent we are trying to contact u todays draw ...
2022     spam  u can win å100 of music gift vouchers every we...
4889      ham  sounds like there could be a lot of time spent...
4526      ham  understand his loss is my gain  so do you work...
1827      ham  hey gorgeous man my work mobile number is have...
3835      ham                   then ì_ come n pick me at 530 ar
342       ham                       where u been hiding stranger
2040      ham        you always make things bigger than they are
1788      ham                        arun can u transfr me d amt
860       ham                  in work now going have in few min
2298      ham  dont pick up d call when something important i...
763       ham  nothing but we jus tot u would ask cos u ba gu...
2475      ham                      mm i am on the way to railway
5156      ham  sir i need velusamy sirs date of birth and com...
164      spam  bangbabes ur order is on the way u should rece...
3671      ham   came to look at the flat seems ok in his 50s ...
4302      ham                                        yup im free

问题是什么?

标签: pythonpandasdataframe

解决方案


查看文档.loc.iloc

您可以尝试使用 if train_data['Category'].iloc[i] == 'ham'

修改后的代码将是:

for i in range(len(phrases)):
    if train_data['Category'].iloc[i] == 'ham':
        print(train_data['Category'].iloc[i])
    elif train_data['Category'].iloc[i] == 'spam':
        print(train_data['Category'].iloc[i])


推荐阅读