Last answered:

31 Dec 2020

Posted on:

28 Dec 2020

0

Encountering error while runnin a code to combine 51 states into 14 categories in credit risk modelling

after checking reason of error so I found paython is not recalling specific state such as when I ran code :

IN [   ]           df_inputs_prepr['addr_state']
OUT [  ]

427211    SC
206088    NJ
136020    GA
412305    MA
36159     CA
Name: addr_state, Length: 373028, dtype: object

 

But when I apply code:

IN  [   ] df_inputs_prepr['addr_state:SC']

OUT [   ]

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2897             try:
-> 2898                 return self._engine.get_loc(casted_key)
   2899             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.PyObjectHashTable.get_item()

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

KeyError: 'addr_state:SC'

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

KeyError                                  Traceback (most recent call last)
<ipython-input-61-eded9de2c028> in <module>
----> 1 df_inputs_prepr['addr_state:SC']

~\anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
   2904             if self.columns.nlevels > 1:
   2905                 return self._getitem_multilevel(key)
-> 2906             indexer = self.columns.get_loc(key)
   2907             if is_integer(indexer):
   2908                 indexer = [indexer]

~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)
   2898                 return self._engine.get_loc(casted_key)
   2899             except KeyError as err:
-> 2900                 raise KeyError(key) from err
   2901 
   2902         if tolerance is not None:

KeyError: 'addr_state:SC'

Because of this my code of creating state categories giving error.

1 answers ( 0 marked as helpful)
Posted on:

31 Dec 2020

0
In time of creating dummies I set  prefix = 'status' instead od 'satate' that is why in time of merging python was unable to recall the spacific state. 

Submit an answer