Pandas for python

Hello, i have to modify an excel list, so i use Python with Pandas, Idon’t know if i’l find an answer on this forum, but I don’t know wich forum is the best to this kind of questions…

do you know how i can concanate a string with the index of the line in a column named ‘CODE’.

and i’d like to format the integer with 9 characters, ‘1’ become ‘000000001’
Ind | Code
00 | C-000000000
01 | C-000000001


Hi @onlyforpeace, see if below helps:

prefix = "C-"
number = 123
digits = 9
output = "{0}{1:0{2}}".format(prefix, number, digits)
print output




1 Like

thank you, but do you know how i can have the index of the line in a dataframe?

Hi @onlyforpeace, unfortunately not. You might post an example definition so others can help.


You could try the Association Francaise Python on or Python Discord

How about this:

d["Ind"] = d.index
d["Code"] = d.Ind.apply(lambda x: f"C-{x:09d}")

thank you, but I wrote it like this:
df_complete['Code du client *']=prefix_codification +'_' + df_complete.index.astype(str).str.zfill(9)

i’ll try your code…
but now it’s holidays!!!

good summer!

1 Like