0

I have a column that I want to add one leading zero to. The zfill() function seems to only allow me to fill to a certain width as shown below:

#numeric as string
df = pd.DataFrame({'ID':['1', '2', '33', '4', 'SS', '15', 'S1', 'A1']})

df['ID'] = df['ID'].str.zfill(2)
print (df)

  ID
0      01
1      02
2      33
3      04
4      SS
5      15
6      S1
7      A1

What I'd like is an ouput that looks like this:

  ID
0      01
1      02
2      033
3      04
4      0SS
5      015
6      0S1
7      0A1

So that no matter what length the string is the zero value added is adjusted to only add to the len(ID) + 1.

Is there an easy way to do this?

HelloToEarth
  • 2,027
  • 3
  • 22
  • 48

0 Answers0