Floating point precision affected when converting DataFrame to list

后端 未结 3 505
情歌与酒
情歌与酒 2021-01-25 08:57

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

相关标签:
3条回答
  • 2021-01-25 09:44

    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.

    0 讨论(0)
  • 2021-01-25 09:45

    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.

    0 讨论(0)
  • 2021-01-25 09:51

    As Padraic alluded, you are not modifying the correct option with display.precision. Instead, try:

    pd.options.display.float_format = '{:,.3f}'.format
    
    0 讨论(0)
提交回复
热议问题