So I\'m trying to convert a float DataFrame to a list (of list) by using row.values.tolist()
(row
was read from a CSV file). It does the job pretty oka
Eventhough, you might still have a problem with precision depending on the base values.
You can still use round
to specify the amount of decimal you wish to have.
df = pd.DataFrame([(.2132481, .399452), (.012311, .13267), (.613216, .01233), (.213211, .181235)])
df.values.round(3).tolist()
>> [[0.213, 0.399], [0.012, 0.133], [0.613, 0.012], [0.213, 0.181]]
The 3
in .round(3)
goes for three decimals.
The "loss of precision" you're seeing is due to the fact that binary floating-point can't precisely represent decimal fractions, so there's some rounding error. If you really want to pass decimal values through unchanged, you'd get better results using an actual decimal representation...
Unfortunately, NumPy doesn't seem to provide any decimal datatypes for you to use.
As Padraic alluded, you are not modifying the correct option with display.precision
. Instead, try:
pd.options.display.float_format = '{:,.3f}'.format