I have a dataset of patient surgery. Many of the patients have had multiple operations and the value_counts aggregation of their multiple operation codes (there are 4 codes) is shown below.
['O011'] 2785
['O012'] 1813
['O011', 'O011'] 811
['O013'] 532
['O012', 'O012'] 522
['O014'] 131
['O013', 'O013'] 125
['O014', 'O014'] 26
['O012', 'O011'] 24
['O011', 'O012'] 20
['O011', 'O011', 'O011'] 14
['O011', 'O013'] 12
['O012', 'O012', 'O011'] 6
['O011', 'O012', 'O012'] 6
['O011', 'O011', 'O011', 'O011'] 5
['O013', 'O013', 'O013'] 5
['O013', 'O011'] 4
['O012', 'O012', 'O012'] 4
['O012', 'O013'] 3
['O013', 'O014'] 3
['O011', 'O013', 'O013'] 3
['O012', 'O014'] 3
['O011', 'O012', 'O011'] 2
['O012', 'O013', 'O013'] 2
['O011', 'O014'] 2
['O013', 'O012', 'O012'] 2
['O014', 'O014', 'O014'] 2
['O013', 'O012'] 1
['O012', 'O012', 'O013', 'O013', 'O013'] 1
['O012', 'O011', 'O012'] 1
['O011', 'O011', 'O012'] 1
['O013', 'O013', 'O011'] 1
['O011', 'O011', 'O012', 'O012'] 1
['O014', 'O013', 'O013'] 1
['O013', 'O013', 'O012'] 1
['O012', 'O011', 'O011'] 1
['O011', 'O012', 'O013'] 1
['O013', 'O011', 'O011'] 1
['O012', 'O012', 'O012', 'O012'] 1
['O013', 'O013', 'O012', 'O012'] 1
['O014', 'O013', 'O011', 'O011'] 1
['O012', 'O011', 'O011', 'O011'] 1
['O013', 'O011', 'O012'] 1
This shows the sequence of their operations by patient count, - so 2785 patients have had just the one procedure, - O012. I want to create a new column with a boolean 'Are all the operations the same'. There is an itertools recipe for comparing the values in a list here I am a surgeon and my python skills are not up to applying it to the series, - how do I create a new column using this function?.
The series is OPERTN_01_list
I tried
from itertools import groupby
def all_equal(iterable):
g = groupby(iterable)
return next(g, True) and not next(g, False)
My dataset is mo
(multiple operations), so I tried to apply the function all_equal
to the series
mo['eq'] = all_equal(mo['OPERTN_01_list'])
but the new column mo['eq']
had all false values.
I am not sure the best way to implement the function.