久久久久久久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 的意思不同)
主站蜘蛛池模板: 一区二区三区中文字幕 | 久久久久久久一区 | 欧美日本在线观看 | 毛片一级片 | 男人天堂免费在线 | 粉嫩av久久一区二区三区 | 久久久性色精品国产免费观看 | 国产一区二区三区www | 亚洲不卡在线观看 | 国产精品久久久久久婷婷天堂 | 夜夜爽夜夜操 | 狠狠亚洲 | 麻豆亚洲| 羞羞视频免费观 | 成年免费大片黄在线观看岛国 | 中文字幕国产精品 | 99色播| 男人天堂av网站 | 超碰精品在线 | 国产高清视频 | 久久亚洲一区二区三区四区 | 欧洲精品在线观看 | 亚洲国产网站 | 亚洲国产一区二区三区 | 国产精品久久久久久影院8一贰佰 | 精品久久久久久亚洲精品 | 神马久久久久久久久久 | 国产精品视频中文字幕 | m豆传媒在线链接观看 | 日韩久久久一区二区 | 日本三级网站在线 | 91精品国产综合久久婷婷香蕉 | 成人欧美一区二区 | 久久精品色欧美aⅴ一区二区 | 在线视频中文字幕 | 视频一区二区三区中文字幕 | 一区二区三区四区电影 | 放个毛片看看 | 精品国产欧美 | 国产一区999 | 国产伦精品一区二区三毛 |