I have a dataframe with 15 separate ICD columns(ICD1
to ICD15
) and want to create a variable "Encep"
(0/1), when the digits "323" appear in any of the 15 ICD columns.
The dataframe itself contains over 30 variables and looks like this
PT_FIN DATE Address... ICD1 ICD2... ICD15
1 July 123 lane 523 432 .
2 August ABC road 523 43.6 12.8
Not entirely sure if I'm on the right track but I wrote the following code in an attempt to accomplish my task but am getting an error:
CODE
ICDA = ["ICD1","ICD2","ICD3","ICD4","ICD5","ICD6","ICD7","ICD8","ICD9","ICD10","ICD11","ICD12","ICD13","ICD14","ICD15"]
ICD1.loc[:,"Encep"]=np.where(ICD1["ICDA"].str.contains("323", case=False),1,0)
ERROR
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
~\AppData\Local\Continuum\anaconda3\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: 'ICDA'
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
<ipython-input-34-564afcae6cd2> in <module>
1 ICDA= ["ICD1","ICD2","ICD3","ICD4","ICD5","ICD6","ICD7","ICD8","ICD9","ICD10","ICD11","ICD12","ICD13","ICD14","ICD15"]
----> 2 ICD1.loc[:,"LumbPCode"]=np.where(ICD1["ICDA"].str.contains("323", case=False),1,0)
~\AppData\Local\Continuum\anaconda3\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]
~\AppData\Local\Continuum\anaconda3\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: 'ICDA'
EDIT
I found a similar question and answer but need to know how to apply this select columns - not the whole dataframe