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