Skip to content Skip to sidebar Skip to footer

How To Create Column Of Ascending Values Based On Unique Values In Another Column In Pandas

I have a dataset where each row is a sample, and a column (name 'Sample_ID') names each sample (df1 below). Some samples are repeated multiple times (i.e. have identical values for

Solution 1:

Use groupby to get each group number and then apply string formatting, eg:

df1['Sample_code'] = df1.groupby('Sample_ID').ngroup().add(1).apply('SAMP{:03}'.format)

Solution 2:

You can create a lookup table by iterating over the unique values and then apply it to a new column:

lookup = {}
for i, sample_name inenumerate(df.Sample_ID.unique()):
    lookup[sample_name] = f'SAMP{i:03}'

df['Sample_code'] = df.Sample_ID.apply(lambda x: lookup[x])

Post a Comment for "How To Create Column Of Ascending Values Based On Unique Values In Another Column In Pandas"