0

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

Dezido
  • 3
  • 2
  • 1
    Have you seen this thread: https://stackoverflow.com/questions/21137150/format-suppress-scientific-notation-from-python-pandas-aggregation-results ? – Taras Oct 21 '21 at 11:35

1 Answers1

0

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
Daweo
  • 31,313
  • 3
  • 12
  • 25