I'm trying to convert a pandas DataFrame to a list of dictionaries in which 1 dictionary represents 1 row; therefore pandas to_dict(orient='records')
method is perfect; however the output is incorrectly rounded in some cases. Here's an example:
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]}
As you can see to_dict(orient='list')
seems to work fine. What's the problem here?