問題描述
我有一個包含數值的 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 passfloat_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模板網!