Given that i have a dataset as below:
dt = {
"facility":["Ann Arbor","Ann Arbor","Detriot","Detriot","Detriot"],
"patient_ID":[4388,4388,9086,9086,9086],
"year":[2004,2007,2007,2008,2011],
"month":[8,9,9,6,2],
"Nr_Small":[0,0,5,12,10],
"Nr_Medium":[3,1,1,4,3],
"Nr_Large":[2,0,0,0,0],
"PeriodBetween2Visits" : [10,0,12,3,1],
"NumberOfVisits" : [2,2,3,3,3]
}
dt = pd.DataFrame(dt)
i need to keep groupby patient_ID
, then keep the facility
,patient_ID
,NumberOfVisits
, but Maximum and minimum of PeriodBetween2Visits
.
Here is what i tried:
dt = dt.groupby(['patient_ID'],as_index=False)["facility","patient_ID","PeriodBetween2Visits","NumberOfVisits"].agg({'PeriodBetween2Visits': ['min', 'max']})
dt.head()
But, it is not what i need!
A proper output for me as below: