pd.read_csv() 保留小数位数

pd.read_csv() keep number of decimals

我想读取 csv,但它剔除了小数位数:

fname = './sol/Pret-SB_A00DLR0_202205240635.pos'
skiprow = 0
with open(fname) as search:
    for i, line in enumerate(search):
        if "%  GPST" in line:
            skiprow = i
            break
df = pd.read_csv(fname, skiprows=skiprow, delim_whitespace=True, parse_dates=[[0, 1]])
df.head(2)

给出(前 2 行,前 5 列):

enter image description here

原始数据(here)第3、4列小数点后8位。我需要那些。

2211 196568.000 -25.732036008 28.282629130 1387.8994
2211 196569.000 -25.732032386 28.282633712 1389.4025

如何读取 csv 并保留原始数据的精度?

How do I read a csv and retain the precision of the original data?

你确实有,pandas只是为了演示目的限制位数,请考虑以下示例

import pandas as pd
df = pd.DataFrame({'x':[28.282633712]})
print(df)
print(df.x[0])
print(df.x[0] == 28.282633712)

给出输出

           x
0  28.282634
28.282633712
True

您可以像这样将显示位数设置为8:

pd.options.display.float_format = "{:,.8f}".format 

读取csv时需要使用'precision'参数

#read text file
pd.read_csv(f,float_precision='round_trip',delimiter = "\t")

#read csv
pd.read_csv(f,float_precision='round_trip')