久久久久久久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 的意思不同)
主站蜘蛛池模板: 亚洲综合色视频在线观看 | 蜜月aⅴ国产精品 | 男女性毛片 | 色欧美综合 | 国产亚韩 | 日本欧美黄色片 | 黄网站在线播放 | 久久久久久免费免费 | 欧美激情视频一区二区三区在线播放 | 久久久.com| 日韩午夜网站 | 欧美日韩成人一区二区 | 亚洲情侣视频 | 伊人色综合久久久天天蜜桃 | 国产精品一区二区三区在线播放 | 欧美二级 | 国产精品99视频 | 久久99精品国产麻豆婷婷 | 蜜桃av鲁一鲁一鲁一鲁 | 产真a观专区| 一区二区三区四区电影视频在线观看 | 国产亚洲精品综合一区 | 一区二区三区国产 | 久久精品一区二区三区四区 | 日韩www | 天堂成人国产精品一区 | 夜夜骑av | 视频在线一区二区 | 国产免费又色又爽又黄在线观看 | 日本一区二区高清不卡 | 欧美成人aaa级毛片在线视频 | 三级视频在线观看电影 | 日本色婷婷 | 久久精品免费观看 | 超碰国产在线 | 欧美在线一区视频 | 9191在线播放| 日韩在线免费视频 | 国产情侣啪啪 | 7777久久| 日韩在线播放中文字幕 |