[英]pandas: to_dict(“records”) rounded incorrectly
我正在尝试将pandas DataFrame转换为字典列表,其中1字典代表1行; 因此,熊猫to_dict(orient='records')
方法是完美的; 但是,在某些情况下,输出的舍入不正确。 这是一个例子:
df = pd.DataFrame({'x': [1/3, 2/3], y=[4/3, 5/3]})
# x y
0 0.333333 1.333333
1 0.666667 1.666667
df.round(3).to_dict(orient='records') # rounded incorrectly
# [{'x': 0.3330000000000002, 'y': 1.333}, {'x': 0.6670000000000004, 'y': 1.667}]
df.round(3).to_dict(orient='list') # rounded correctly
# {'x': [0.333, 0.667], 'y': [1.333, 1.667]}
如您所见, to_dict(orient='list')
似乎工作正常。 这是什么问题
在pandas 0.20.2中,出于某些原因, orient = records
使用numpy浮点类型,而orient = list
使用本机python浮点类型。
records = df.round(3).to_dict(orient='records')
print(type(records[0]['x']))
numpy.float64
list_orient=df.round(3).to_dict(orient='list')
print(type(list_orient['x'][0]))
float
确切数据类型的差异导致舍入差异。 现在,为什么不能说不同的Orient参数导致不同的数据类型呢?
将numpy浮点数转换回本机python浮点数时:
print(float(records[0]['x']))
0.333
我们得到的输出类似于面向to_records输出的列表中的输出。
有关奇怪的float恶作剧的更多信息, 浮点数学是否已损坏?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.