boyenec
boyenec

Reputation: 1617

python Pandas how to set my custom index combining string and number

I have an dataframe like this:

product_nmae
Productashd
productddsf
productkid

my expected dataframe will be look like this:

  product_nmae    unique_id
    Productashd    sku-1
    productddsf    sku-2
    productkid    sku-3

I tried this df['unique_id'] = df.index + 1 which giving result something like this

  product_nmae    unique_id
    Productashd       0
    productddsf       1
    productkid        3

how to get my expected result?

Upvotes: 1

Views: 103

Answers (1)

jezrael
jezrael

Reputation: 863341

Create default index and for possible add sku convert to strings:

df = df.reset_index(drop=True)
df['unique_id'] = 'sku-' + (df.index + 1).astype(str)

Upvotes: 4

Related Questions