简体   繁体   中英

pd.read_csv() keep number of decimals

I want to read a csv but it culls the number of decimals:

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)

gives (first 2 rows, first five columns):

enter image description here

the original data ( here ) has 8 decimal places in the 3rd and 4th columns. I need those.

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

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

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

You do have it, pandas simply limit number of digits for presentation purposes, consider following example

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

gives output

           x
0  28.282634
28.282633712
True

You can set the number of displayed digits to 8 like this:

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

You need to use 'precision' parameter while reading csv

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM