久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Pandas 讀取具有浮點值的 csv 文件會導致奇怪的舍

Pandas read csv file with float values results in weird rounding and decimal digits(Pandas 讀取具有浮點值的 csv 文件會導致奇怪的舍入和小數位數)
本文介紹了Pandas 讀取具有浮點值的 csv 文件會導致奇怪的舍入和小數位數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個包含數值的 csv 文件,例如 1524.449677.總有 6 位小數.

I have a csv file containing numerical values such as 1524.449677. There are always exactly 6 decimal places.

當我通過 pandas read_csv 導入 csv 文件(和其他列)時,該列會自動獲取數據類型 object.我的問題是這些值顯示為 2470.6911370000003 實際上應該是 2470.691137.或者值 2484.30691 顯示為 2484.3069100000002.

When I import the csv file (and other columns) via pandas read_csv, the column automatically gets the datatype object. My issue is that the values are shown as 2470.6911370000003 which actually should be 2470.691137. Or the value 2484.30691 is shown as 2484.3069100000002.

這在某種程度上似乎是一個數據類型問題.在通過 read_csv 導入時,我嘗試通過將 dtype 參數作為 {'columnname': np.float64} 來顯式提供數據類型.問題仍然沒有消失.

This seems to be a datatype issue in some way. I tried to explicitly provide the data type when importing via read_csv by giving the dtype argument as {'columnname': np.float64}. Still the issue did not go away.

如何獲取導入的值并完全按照它們在源 csv 文件中的樣子顯示?

How can I get the values imported and shown exactly as they are in the source csv file?

推薦答案

Pandas 使用專用的 dec 2 bin 轉換器,該轉換器會犧牲準確性而不是速度.

Pandas uses a dedicated dec 2 bin converter that compromises accuracy in preference to speed.

float_precision='round_trip' 傳遞給 read_csv 可以解決此問題.

Passing float_precision='round_trip' to read_csv fixes this.

查看 此頁面 了解更多詳情.

Check out this page for more detail on this.

處理完你的數據后,如果你想把它保存回一個csv文件,你可以將
float_format = "%.nf"傳給對應的方法.

After processing your data, if you want to save it back in a csv file, you can pass
float_format = "%.nf" to the corresponding method.

一個完整的例子:

import pandas as pd

df_in  = pd.read_csv(source_file, float_precision='round_trip')
df_out = ... # some processing of df_in
df_out.to_csv(target_file, float_format="%.3f") # for 3 decimal places

這篇關于Pandas 讀取具有浮點值的 csv 文件會導致奇怪的舍入和小數位數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點小數點/精度)
Converting Float to Dollars and Cents(將浮點數轉換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計算可以返回 NaN?)
Python float to ratio(Python浮動比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 久久99精品久久久水蜜桃 | 日韩精品一区二区三区视频播放 | 日韩不卡一区二区 | 日韩国产中文字幕 | 成人国产精品久久 | 国产在线视频在线观看 | 91麻豆精品国产91久久久更新资源速度超快 | 国产精品美女在线观看 | 国产亚洲欧美在线 | 一区二区三区高清 | 中文字幕一区二区三区四区五区 | 国产高清一区二区三区 | 中文字幕av高清 | 亚洲综合小视频 | 成人av一区二区在线观看 | 精品日韩一区二区 | 欧美性生交大片免费 | 伊人伊人| 亚洲小说图片 | 日韩欧美在线不卡 | 亚洲精品白浆高清久久久久久 | 久久亚洲一区二区 | a在线视频 | 玖玖免费 | 久久九九影视 | 国产一区黄色 | 美女黄网| 日韩色图在线观看 | 激情网五月天 | 亚洲欧美日韩激情 | 久久精品国产亚洲 | 久久神马 | 午夜精品久久久久久久久久久久久 | 成人精品 | 国产精品久久久久久久免费观看 | 激情视频一区 | 天天影视综合 | 国产伦精品一区二区三区高清 | www.youjizz.com日韩| 日韩免费1区二区电影 | 一区二区三区四区在线 |