pythonpandascsvexponential

How to save values to a CSV file in exponent in python


For a project in Python i have to read float values from an excel file and save it as exponential values in a CSV file by using Pandas. When i print the values, they are shown as exponent, but when i open the CSV file afterwards, i get only float values. How to save them as exponential values?

    Datas = pd.read_excel(Dateiname_Excel, sheet_name = "Tabelle1", header=0)
    print (Datas)

    pd.set_option('display.float_format', '{:.1E}'.format)
    print (Datas)

    Datas_2 = Datas.astype(float, '{:.1E}'.format)
    Datas_2["Pos"] = Datas_2["Pos"].astype(int)

    Datas_2.to_csv(Dateiname_CSV, index = None, header = HeaderList)
    print (Datas_2)

    CSV_File.close()

Solution

  • The formatting you applied with pd.set_option('display.float_format', '{:.1E}'.format) is for display purposes only and does not affect how data is written to files. I try to correct it for you

    enter code here
       import pandas as pd
       Datas = pd.read_excel(Dateiname_Excel, sheet_name="Tabelle1", header=0)
       Datas_2 = Datas.copy()
       for col in Datas_2.select_dtypes(include=['float']):
          Datas_2[col] = Datas_2[col].apply(lambda x: f'{x:.1E}')
       if 'Pos' in Datas_2.columns:
          Datas_2["Pos"] = Datas_2["Pos"].astype(int)
       Datas_2.to_csv(Dateiname_CSV, index=None, header=HeaderList)
       print(Datas_2)