Sparse Matrix Creation : KeyError: 579 for text datasets

I am trying to use the make_sparse_matrix function to create a sparse matrix for my text dataset, and I face KeyError: 579. Does anyone has any leads on the root of the error.

def make_sparse_matrix(df, indexed_words, labels):
"""
Returns sparse matrix as dataframe.

df: A dataframe with words in the columns with a document id as an index (X_train or X_test)
indexed_words: index of words ordered by word id
labels: category as a series (y_train or y_test)
"""

nr_rows = df.shape[0]
nr_cols = df.shape[1]
word_set = set(indexed_words)
dict_list = []

for i in range(nr_rows):
    for j in range(nr_cols):
        
        word = df.iat[i, j]
        if word in word_set:
            doc_id = df.index[i]
            word_id = indexed_words.get_loc(word)
            category = labels.at[doc_id]
            
            item = {'LABEL': category, 'DOC_ID': doc_id,
                   'OCCURENCE': 1, 'WORD_ID': word_id}
            
            dict_list.append(item)

return pd.DataFrame(dict_list)



make_sparse_matrix( X_train, word_index,  y_test ) 

X_train is a DF that contains one single word in each cell, word_index contains all the index of words and y_test stores all labels.

The Key Error I am facing is:

--------------------------------------------------------------------------- KeyError Traceback (most recent call last) ~\New folder\envs\geo_env\lib\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: 579

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

KeyError Traceback (most recent call last) in

in make_sparse_matrix(df, indexed_words, labels) 20 doc_id = df.index[i] 21 word_id = indexed_words.get_loc(word) ---> 22 category = labels.at[doc_id] 23 24 item = {'LABEL': category, 'DOC_ID': doc_id,

~\New folder\envs\geo_env\lib\site-packages\pandas\core\indexing.py in getitem(self, key) 2154 return self.obj.loc[key] 2155 -> 2156 return super().getitem(key) 2157 2158 def setitem(self, key, value):

~\New folder\envs\geo_env\lib\site-packages\pandas\core\indexing.py in getitem(self, key) 2101 2102 key = self._convert_key(key) -> 2103 return self.obj._get_value(*key, takeable=self._takeable) 2104 2105 def setitem(self, key, value):

~\New folder\envs\geo_env\lib\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

~\New folder\envs\geo_env\lib\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: 579