-2

I have a similiar question to this, but it is not about the visualization. I have a pandas dataframe with an int column.

A
5
43
1200
555

And I need to have always 4 digits, as this stands original for a timestamp:

00:05
00:43
12:00
05:55

How can i work on that column that i have the outcome:

0005
0043
1200
0555
Georgy
  • 12,464
  • 7
  • 65
  • 73
PV8
  • 5,799
  • 7
  • 43
  • 87
  • So qustion is only about `df['A'] = df['A'].astype(str).str.zfill(4)` ? Not added `:` ? – jezrael Feb 14 '20 at 13:22
  • 1
    the add on with the : is nice, that helped, i changed my question – PV8 Feb 14 '20 at 13:52
  • Please, don't change the meaning of the question. If you have another question, you should ask it in a separate post. – Georgy Feb 14 '20 at 17:18

1 Answers1

2

Use Series.str.zfill, but first convert to strings:

df['A'] = df['A'].astype(str).str.zfill(4)
print (df)
      A
0  0005
1  0043
2  1200
3  0555

If need ::

s = df['A'].astype(str).str.zfill(4)
df['A'] = s.str[:2] + ':' + s.str[2:]

Or:

df['A'] = df['A'].astype(str).str.zfill(4).map(lambda x: f'{x[:2]}:{x[2:]}')

print (df)
       A
0  00:05
1  00:43
2  12:00
3  05:55
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252