I have the following data frame, which I need to use the aggregate function for one specific column which I am listing in the value. I am using pivot table from pandas for that.
Sample ID Type Score Freq
AE01 AAA Non 0.65 1
AE01 BBB IND 0.57 14
AE03 SAS IND 0.56 14
AE03 SAP IND 0.689 15
AE03 TCS IND 0.56 16
AE05 BBB IND 0.85 17
AE05 CTC IND 0.45 18
AE05 CTC Non 0.15 19
AE05 CTC Non 0.14 20
AE05 CTC Non 0.4678 21
The following is the script I used for that,
table_pat_rel = pd.pivot_table(df,index=["ID",'Type'],values=['Sample'],
aggfunc={'Sample':np.size})
Give some following output,
ID Type Sample
AAA Non 1
BBB IND 2
SAS IND 1
SAP IND 1
TCS IND 1
CTC IND 5
But I am aiming following output,
ID Recurrence Sample
AAA 1 AE01
BBB 2 AE01
AE05
SAS 1 AE03
SAP 1 AE03
TCS 1 AE03
CTC 4 AE05
I tried with groupby as following
df.drop_duplicates(['Sample', 'ID']).groupby(['ID','Sample']).size().sort_values(ascending=True).head()