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

Time:08-15

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?

CodePudding user response:

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)
  • Related