Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create an index that is both string and number for a dataframe?

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading