🛠️ Scheduled Maintenance | We’ll be undergoing scheduled maintenance and upgrades between 00:00 PST Jan 26th until 00:00 PST Jan 28th. There may be brief interruption of services in that period. We apologize for the inconvenience.

×
The 365 Data Science team is proud to invite you to our own community forum. A very well built system to support your queries, questions and give the chance to show your knowledge and help others in their path of becoming Data Science specialists.
Ask
Anybody can ask a question
Answer
Anybody can answer
Vote
The best answers are voted up and moderated by our team

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

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

0
Votes
1
Answer

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 Answer

0
Votes

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.