Posts: 119 Threads: 66 Joined: Sep 2022 Hi Team, I am trying to write pandas Dataframe into csv, wrong values getting printed into csv. exponent issue. -5.00E-06 import pandas as pd Net_Income = [0.00000,-0.000005,-1.859603,-55.788084] df = pd.Series(Net_Income) df.to_csv(r'E:\data\output.csv',mode='w',sep='|',index=False) output 0 0 -5.00E-06 <------ how to fix this issue -1.859603 -55.788084 Posts: 7,398 Threads: 123 Joined: Sep 2016 To avoid to be shorten to scientific notation(n-5e-06),use float_format parameter. >>> print(df.to_csv(index=False, line_terminator='\n', float_format='%.6f')) 0 0.000000 -0.000005 -1.859603 -55.788084 Posts: 453 Threads: 16 Joined: Jun 2022 Jan-20-2023, 06:46 PM (This post was last modified: Jan-20-2023, 06:46 PM by rob101.) Try using the float_format="%.6f" option, as an example. To add: I see that snippsat beat me to it! Sig: >>> import this The UNIX philosophy: "Do one thing, and do it well." "The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse "Everything should be made as simple as possible, but not simpler." :~ Albert Einstein Posts: 119 Threads: 66 Joined: Sep 2022 Jan-20-2023, 07:10 PM (This post was last modified: Jan-20-2023, 07:10 PM by mg24.) Hi Team, pandas dataframe prints values correctly in console window. but when I try to write data into csv files , I see Exponent symbols in csv. Actual issue I face when I try to import csv files into sql table. conversation issue I face. Posts: 453 Threads: 16 Joined: Jun 2022 If I take you example code and write the csv to a console rather than to file... Net_Income = [0.00000, -0.000005, -1.859603, -55.788084] df = pd.Series(Net_Income) print(df) print() x = df.to_csv(float_format="%.6f") print(x) ... I see this: Output: df 0 0.000000 1 -0.000005 2 -1.859603 3 -55.788084 dtype: float64 csv ,0 0,0.000000 1,-0.000005 2,-1.859603 3,-55.788084
There should be no difference when writing to a file. Sig: >>> import this The UNIX philosophy: "Do one thing, and do it well." "The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse "Everything should be made as simple as possible, but not simpler." :~ Albert Einstein Posts: 119 Threads: 66 Joined: Sep 2022 Hi Rob, in both cases you are printing values into console window. if you check actual data into csv you come across some changes in data. exponent value comes. these csv values When I try to import back into SQL , there I face conversation issue. Thanks mg Posts: 453 Threads: 16 Joined: Jun 2022 (Jan-20-2023, 07:33 PM)mg24 Wrote: if you check actual data into csv you come across some changes in data. exponent value comes. Done, but as I suspected, I see the exact same output in the file as I do on my console. Sig: >>> import this The UNIX philosophy: "Do one thing, and do it well." "The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse "Everything should be made as simple as possible, but not simpler." :~ Albert Einstein Posts: 119 Threads: 66 Joined: Sep 2022 Hi Rob. your code seems works. if I increase width in excels cells, exponent issue goes. is there any pythonic way to auto adjust excels cell widths. in vba it has a feature. -0.000005 Posts: 6,920 Threads: 22 Joined: Feb 2020 Same here. Running this code: import pandas as pd df = pd.Series([0.00000, -0.000005, -1.859603, -55.788084]) df.to_csv("test.csv", sep="|", index=False, float_format="%.6f")I get this in the csv file. This is not console output. Output: 0 0.000000 -0.000005 -1.859603 -55.788084 Posts: 453 Threads: 16 Joined: Jun 2022 (Jan-20-2023, 08:04 PM)mg24 Wrote: your code seems works. if I increase width in excels cells, exponent issue goes. That would explain it: Excel will (by default) reformat numbers to fit the cell width. You see, when you say CSV, I take that as a literal meaning and I almost never view CSV files in a spreadsheet; rather I use a text editor. Sig: >>> import this The UNIX philosophy: "Do one thing, and do it well." "The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse "Everything should be made as simple as possible, but not simpler." :~ Albert Einstein |