在pandas中添加带前缀的唯一标识符列



我试图在pandas DataFrame中添加一个具有前缀"ACC"的唯一列。我怎么做呢?

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

如果存在唯一的索引值,则使用:

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

对于任何索引的另一个想法:

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))]

最新更新