問(wèn)題描述
我有一個(gè)包含數(shù)值的 csv 文件,例如 1524.449677
.總有 6 位小數(shù).
I have a csv file containing numerical values such as 1524.449677
. There are always exactly 6 decimal places.
當(dāng)我通過(guò) pandas read_csv
導(dǎo)入 csv 文件(和其他列)時(shí),該列會(huì)自動(dòng)獲取數(shù)據(jù)類型 object
.我的問(wèn)題是這些值顯示為 2470.6911370000003
實(shí)際上應(yīng)該是 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
.
這在某種程度上似乎是一個(gè)數(shù)據(jù)類型問(wèn)題.在通過(guò) read_csv
導(dǎo)入時(shí),我嘗試通過(guò)將 dtype
參數(shù)作為 {'columnname': np.float64}
來(lái)顯式提供數(shù)據(jù)類型.問(wèn)題仍然沒(méi)有消失.
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.
如何獲取導(dǎo)入的值并完全按照它們?cè)谠?csv 文件中的樣子顯示?
How can I get the values imported and shown exactly as they are in the source csv file?
推薦答案
Pandas 使用專用的 dec 2 bin
轉(zhuǎn)換器,該轉(zhuǎn)換器會(huì)犧牲準(zhǔn)確性而不是速度.
Pandas uses a dedicated dec 2 bin
converter that compromises accuracy in preference to speed.
將 float_precision='round_trip'
傳遞給 read_csv
可以解決此問(wèn)題.
Passing float_precision='round_trip'
to read_csv
fixes this.
查看 此頁(yè)面 了解更多詳情.
Check out this page for more detail on this.
處理完你的數(shù)據(jù)后,如果你想把它保存回一個(gè)csv文件,你可以將float_format = "%.nf"
傳給對(duì)應(yīng)的方法.
After processing your data, if you want to save it back in a csv file, you can passfloat_format = "%.nf"
to the corresponding method.
一個(gè)完整的例子:
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
這篇關(guān)于Pandas 讀取具有浮點(diǎn)值的 csv 文件會(huì)導(dǎo)致奇怪的舍入和小數(shù)位數(shù)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!