Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was trying to separate the details in a column of a dataframe based on the separator ','. I was splitting the column into the following new columns 'cast_id','character','credit_id', 'id','name','order','profile_path.
The original column is by the name of 'cast' in the train data set.

I am getting the following error.

KeyError                                  Traceback (most recent call last)
c:\users\user\appdata\local\programs\python\python37\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2889             try:
-> 2890                 return self._engine.get_loc(key)
   2891             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.PyObjectHashTable.get_item()

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

KeyError: 'cast_id'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-12-bace91ca4006> in <module>
----> 1 train_data=pd.concat([train_data['cast_id'],train_data['character'],train_data['credit_id'],train_data['gender'],train_data['id'],train_data['name'],train_data['order'],train_data['profile_path'],train_data[17].str.split(',',expand=True)],axis=1)
      2 train_data

c:\users\user\appdata\local\programs\python\python37\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2973             if self.columns.nlevels > 1:
   2974                 return self._getitem_multilevel(key)
-> 2975             indexer = self.columns.get_loc(key)
   2976             if is_integer(indexer):
   2977                 indexer = [indexer]

c:\users\user\appdata\local\programs\python\python37\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2890                 return self._engine.get_loc(key)
   2891             except KeyError:
-> 2892                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2893         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2894         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.PyObjectHashTable.get_item()

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

KeyError: 'cast_id'


What I have tried:

train_data=pd.concat([train_data['cast_id'],train_data['character'],train_data['credit_id'],train_data['gender'],train_data['id'],train_data['name'],train_data['order'],train_data['profile_path'],train_data[17].str.split(',',expand=True)],axis=1)
train_data
Posted
Comments
Richard MacCutchan 19-Oct-19 9:19am    
The error is telling you that the key cast_id does not exist. Look at the original data in variable train_data and see why this is not being created.
Member 14557966 20-Oct-19 7:14am    
I have checked it all, sir, but unfortunately nothing is working. If you want I can upload the original train data set as well.
Richard MacCutchan 20-Oct-19 9:25am    
Just edit your question and let us see the part of the code where the train_data set is created, and the data that is used to populate it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900