小编Jos*_*uez的帖子

将round float转换为string pandas的问题

我正在将数据框中的列舍入到最近的5个浮点.之后我将列值转换为字符串,但是当我这样做时,浮动返回就好像它们没有接地一样.我正在使用Python 2.7

import pandas as pd
df=pd.read_excel(C:"path")

def custom_round(x, base=5):
    return base * round(float(x)/base)

df['A'] = df['kl'].apply(lambda x: custom_round(x, base=.05))
df['b'] = "D = "  + df['A'].astype(str)


     kl     A                       b  
 0.600001  0.60  D = 0.6000000000000001  
 0.600001  0.60  D = 0.6000000000000001  
 0.600000  0.60  D = 0.6000000000000001  
 0.600000  0.60  D = 0.6000000000000001  
 0.600587  0.60  D = 0.6000000000000001  
 0.601573  0.60  D = 0.6000000000000001  
 0.601168  0.60  D = 0.6000000000000001  
 0.600001  0.60  D = 0.6000000000000001  
 0.600001  0.60  D = 0.6000000000000001  
 0.600001  0.60  D = 0.6000000000000001 …
Run Code Online (Sandbox Code Playgroud)

floating-point rounding dataframe python-2.7 pandas

7
推荐指数
1
解决办法
200
查看次数

标签 统计

dataframe ×1

floating-point ×1

pandas ×1

python-2.7 ×1

rounding ×1