I have a dataframe called clean, which is then separated in two samples: train_data and test_data, with the code below :
train_data = clean.sample(frac=0.75)
test_data = clean.drop(train_data.index)
I am trying to make a word frequency dataframe from the train_data dataframe. I started with the code
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])
But it gives me an error on the line with  if train_data['Category'][i] == 'ham': when the index i is not in the 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
The train_data looks like this (first 20 rows):
  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
What is the issue?
 
     
     
    