Home > Mobile >  insert sub-id column into pandas dataframe
insert sub-id column into pandas dataframe

Time:06-23

I have the following pandas dataframe:

ID  TYPE    ESC END
TRL EC1 MISL1123    36
TRL EC2 XISL1124    57
LBL EC1 CARB24  20
LBL EC1 AARB70  96
LBL EC2 MUT23   79

I want to insert a column of sequential sub-ids (Column "SEQUENCE") into the dataframe to account for multiple enteries in column "ID":

ID  TYPE    SEQUENCE    ESC END
TRL EC1 seq0    MISL1123    36
TRL EC2 seq1    XISL1124    57
LBL EC1 seq0    CARB24  20
LBL EC1 seq1    AARB70  96
LBL EC2 seq2    MUT23   79

Here's my attempt that didn't work:

df['SEQUENCE']=df.groupby(df['ID', 'TYPE']).cumsum().astype(int))

What changes can I make to get this code to work? Thanks

CodePudding user response:

Does this accomplish what you needed?

df['Sequence'] = 'seq'   df.groupby('ID').cumcount().astype(str)
  • Related