Pandas - Add leading “0” to string values so all values are equal len

前端 未结 3 1245
無奈伤痛
無奈伤痛 2021-01-26 21:00

I have a column, code_x. I converted it to .astype(str). Some example values are 45362.0, 75345.0, 346157.0, 572575.0. I would like them a

3条回答
  •  轮回少年
    2021-01-26 21:50

    IIUC, you can use str.zfill which means you wouldn't have to special-case by length:

    In [16]: ser
    Out[16]: 
    0     45362
    1     75345
    2    346157
    3    572575
    dtype: float64
    
    In [17]: ser.astype(str).str.zfill(8)
    Out[17]: 
    0    045362.0
    1    075345.0
    2    346157.0
    3    572575.0
    dtype: object
    

提交回复
热议问题