简体   繁体   中英

problem to concatenate the index of a pandas series with a string

I would like to have the index of row concanate to the a string in a dataframe column, like C-00000001 where C- is the string and 01 is the index format with 9 characters... i tried a lot of thing without solution...

df_complete['Code du client *']="C-"+ str(df_complete.index)

str(…) will give you a unique string, that will be broadcasted to all rows.

You need to use a vectorial transformation with astype :

df_complete['Code du client *'] = 'C-' + df_complete.index.astype(str)

If you have a range index and want to ensure 9 characters, use zfill :

'C-' + df_complete.index.astype(str).str.zfill(9)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM