Search code examples
pandasuniqueidentifierprefix

Adding unique identifier column with prefix in pandas


I am trying to add a unique column in pandas DataFrame with prefix "ACC". How do I do that?

i.e

City               New column,      
Atlanta            ACC-1,
Newyork            ACC-2,

Solution

  • If there is unique index values use:

    df['New column'] = 'ACC-' + (df.index + 1).astype(str)
    

    Another idea for any index:

    df['New column'] = np.arange(1, len(df) + 1)
    df['New column'] = 'ACC-' + df['New column'].astype(str)
    

     df['New column'] = [f'ACC-{i+1}' for i in range(len(df))]