how to remove zeros after decimal from string remove all zero after dot

后端 未结 5 1216
傲寒
傲寒 2021-01-15 09:13

I have data frame with a object column lets say col1, which has values likes: 1.00, 1, 0.50, 1.54

I want to have the output like the below: 1, 1, 0.5, 1.54 basically

5条回答
  •  醉梦人生
    2021-01-15 09:39

    I think something like this should work:

    if val.is_integer() == True :
        val = int(val)
    elif val.is_float() == True :
        val = Decimal(val).normalize()
    

    Assuming that val is a float value inside the dataframe's column. You simply cast the value to be integer. For float value instead you cut extra zeros.

提交回复
热议问题