如何在pandas数据帧中显示特定数字

Hei*_*erg 4 python dataframe pandas

我有如下数据框

   month
0    1
1    2
2    3
3   10 
4   11 
Run Code Online (Sandbox Code Playgroud)

例如,我想像这样以2位数显示这个数据帧

     month
0     01
1     02
2     03
3     10
4     11
Run Code Online (Sandbox Code Playgroud)

我尝试了很多方法,但效果不佳.我怎样才能得到这个结果?

jez*_*ael 6

你可以使用str.zfill:

print (df['month'].astype(str).str.zfill(2))
0    01
1    02
2    03
3    10
4    11
Name: month, dtype: object
Run Code Online (Sandbox Code Playgroud)