Search code examples
pythonpandascsvscientific-notation

Converting .csv file into scientific notation with pandas


I'm trying to convert csv files with pandas.

def convert_file(file, columns, name):
    df = pd.read_csv(file, header=0, delimiter=',')
    df.drop(df.columns[columns], axis=1, inplace=True)
    df.to_csv(name, index=False, header=False)

After dropping the columns I don't need I want to bring the numbers into scientific format. What options do I have?

Example:

current result -> desired result

0.0053455117 -> 5.3455117e-003

0.88455491 -> 8.8455491e-001

10.576477 -> 1.0576477e+001


Solution

  • What options do I have?

    Harness float_format of .to_csv. Consider following example

    import pandas as pd
    df = pd.DataFrame({'x':[0.1,0.01,0.001]})
    df.to_csv("file.csv",float_format="%e",header=False,index=False)
    

    produces file.csv

    1.000000e-01
    1.000000e-02
    1.000000e-03