Home > Back-end >  How to create an index that is both string and number for a dataframe?
How to create an index that is both string and number for a dataframe?

Time:01-18

I wish to create a column just like this:

enter image description here

İn Excel this can be done easily where the remaining rows can be auto-filled. How to achieve the same in python using pandas?

CodePudding user response:

Use f-strings with zero fill values:

df['new'] = [f'abc_{n 1:02}' for n in range(len(df))]

If default index is possible add 1, convert to strings and add str.zfill:

df['new'] = 'abc_'   (df.index   1).astype(str).str.zfill(2)
  •  Tags:  
  • Related