How to display a specific digit in a pandas dataframe

I have a dataframe as below

   month
0    1
1    2
2    3
3   10 
4   11 

for example, I would like to display this data file in 2 digits, like this

     month
0     01
1     02
2     03
3     10
4     11

I tried many methods but did not work. How can I get this result?

+4
source share
2 answers

You can use str.zfill:

print (df['month'].astype(str).str.zfill(2))
0    01
1    02
2    03
3    10
4    11
Name: month, dtype: object
+6
source

I would choose @jezrael's answer to this, but I also like this answer

df.month.apply('{:02d}'.format)

0    01
1    02
2    03
3    10
4    11
Name: month, dtype: object
+2
source

Source: https://habr.com/ru/post/1657468/


All Articles