Home > Blockchain >  How I find drug_code sequence for each pateint in their treatment?
How I find drug_code sequence for each pateint in their treatment?

Time:12-26

Experts, Even though every patient comes to the hospital for the treatment of the same disease, his/her sequence of treatment is slightly different based on their current conditions. Example: Few patients may need more pre-care/pre-medicines than other patients. Here our objective is how to collect all sequences of treatments and quantify those patterns. Please help me if you can. My Pandas knowledge is not enough for solving this problem :-(

Current Dataset:

df2 = pd.DataFrame({'patient: ['one', 'one', 'one', 'two','two', 'two','three','three', 'three'],    
                     'drug_code': ['011', '012', '013', '012', '013', '011','011', '012', '013'],        
                     'date': ['11/20/2022', '11/22/2022', '11/23/2022', '11/8/2022', '11/9/2022', '11/14/2022','11/8/2022', '11/9/2022', '11/14/2022']})

df2['date'] = pd.to_datetime(df2['date'])

Result Dataset like to have to find pattern sequences:

code_patterns = pd.DataFrame({'pattern':['011-012-013','012-013-011'],
                             'frequency': [2,1]})

CodePudding user response:

Try to group by patient with concatenating drug_code values into sequences, then find counts of the same drug sequences among users:

drug_freq_df = df2.groupby(['patient']).apply(lambda x: '-'.join(x['drug_code']))\
    .value_counts().to_frame(name='frequency')\
    .rename_axis('pattern').reset_index()

print(drug_freq_df)

The output:

       pattern  frequency
0  011-012-013          2
1  012-013-011          1 
  • Related