I am a doctor looking at surgery data. There is a column in the data frame for admission method.
df['ADMIMETH']
There are a number of codes for admission method, but the categories essentially are emergency or non-emergency.
Emergency has 17 codes, non-emergency has 3 codes. I want to create a new column with the categories emergency, non-emergency by filtering the admission method column. I have used a categorizing method - writing a function to categorize the admission methods into emergency or non-emergency, and then applied that function to the relevant pandas column.
emerg = ['2A', '2B', '2C', '2D', '2C', '28', '31', '32', '21', '22', '23', '24', '25', '2A', '2B', '2C', '2D']
nonemerg = ['11', '12', '13']
def filter(x):
if df['ADMIMETH'].isin(emerg):
return 'acute'
if df['ADMIMETH'].isin(nonemerg):
return 'elective'
df['new_col'] = df['ADMIMETH'].apply(filter)
File ~/Library/Python/3.9/lib/python/site-packages/pandas/_libs/lib.pyx:2918, in pandas._libs.lib.map_infer()
Cell In [7], line 2, in filter(x)
1 def filter(x):
----> 2 if df['ADMIMETH'].isin(emerg):
3 return 'acute'
4 if df['ADMIMETH'].isin(nonemerg):
File ~/Library/Python/3.9/lib/python/site-packages/pandas/core/generic.py:1527, in NDFrame.__nonzero__(self)
1525 @final
1526 def __nonzero__(self) -> NoReturn:
-> 1527 raise ValueError(
1528 f"The truth value of a {type(self).__name__} is ambiguous. "
1529 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
1530 )
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
as above - value error. I have obviously done soething funadamentally wrong. I would like to keep it as simple as possible as I am a doctor not a developer...