Search code examples
python-3.xpandasdataframetabulate

How to read text file data in scientific format using pandas DataFrame


I have a text file (input.txt) with 2 columns of data which is in scientific format as shown.

input.txt file contents:
4.6277245181485196e-02 -3.478992280123e-02
5.147225314664928553e-02 -3.626645537995224627e-02
5.719622597261836416e-02 -3.778369677696073736e-02
6.351385032440140521e-02 -3.9348512512335400e-02
7.049988917103996999e-02 -4.096034949794334634e-02
7.822948857937785105e-02 -4.261684461302106541e-02
8.67649433797989394455e-02 -4.77e-02
9.614380281036348508e-02 -4.604114963738591831e-02
1.063651118650106309e-01 -4.777947266421164740e-02
1.173824105396738815e-01 -4.950717696170207904e-02
1.291006932795119577e-01 -5.119743181445588626e-02

I used below code to read the data as a DataFrame.

import pandas as pd
from tabulate import tabulate

df = pd.read_csv('input.txt',delim_whitespace=True,engine='python',header=None,skip_blank_lines=True)
f=open('output.txt','w')
f.write(tabulate(df.values,tablefmt="plain"))
f.close()

But the data is not getting read in scientific format. I'm writing the same data to another outfile file using tabulate (to look evenly spaced as a table). And, it is not in scientific format and also truncating the digits as shown.

output.txt file contents:
0.0462772  -0.0347899
0.0514723  -0.0362665
0.0571962  -0.0377837
0.0635139  -0.0393485
0.0704999  -0.0409603
0.0782295  -0.0426168
0.0867649  -0.0477
0.0961438  -0.0460411
0.106365   -0.0477795
0.117382   -0.0495072
0.129101   -0.0511974

I need the data to be read as-is, i.e. scientific format in this case and output to another file using tabulate. What needs to modify in the above code?


Solution

  • When reading the CSV specify dtype=str:

    df = pd.read_csv("input.txt", sep=r"\s+", engine="python", dtype=str, header=None)
    print(tabulate(df.values, tablefmt="plain", disable_numparse=True))
    

    Prints:

    4.6277245181485196e-02      -3.478992280123e-02
    5.147225314664928553e-02    -3.626645537995224627e-02
    5.719622597261836416e-02    -3.778369677696073736e-02
    6.351385032440140521e-02    -3.9348512512335400e-02
    7.049988917103996999e-02    -4.096034949794334634e-02
    7.822948857937785105e-02    -4.261684461302106541e-02
    8.67649433797989394455e-02  -4.77e-02
    9.614380281036348508e-02    -4.604114963738591831e-02
    1.063651118650106309e-01    -4.777947266421164740e-02
    1.173824105396738815e-01    -4.950717696170207904e-02
    1.291006932795119577e-01    -5.119743181445588626e-02