Floating point precision affected when converting DataFrame to list

后端 未结 3 533
情歌与酒
情歌与酒 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条回答
  •  Happy的楠姐
    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.

提交回复
热议问题