Python Forum
pandas dataframe into csv .... exponent issue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pandas dataframe into csv .... exponent issue
#1
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
Reply
#2
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
Reply
#3
Try using the float_format="%.6f" option, as an example.

To add: I see that snippsat beat me to it! Cool
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
Reply
#4
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.
Reply
#5
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
Reply
#6
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
Reply
#7
(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
Reply
#8
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
Reply
#9
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
Reply
#10
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  I'm looking for the syntax of a complex exponent, like: 2 ** (n - m) CHANCEMAN 2 543 Dec-29-2023, 01:53 PM
Last Post: Yoriz
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 734 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  Question on pandas.dataframe merging two colums shomikc 4 833 Jun-29-2023, 11:30 AM
Last Post: snippsat
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 5,642 Feb-17-2023, 06:01 PM
Last Post: Sameer33
  help how to get size of pandas dataframe into MB\GB mg24 1 2,372 Jan-28-2023, 01:23 PM
Last Post: snippsat
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 836 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  How to retrieve records in a DataFrame (Python/Pandas) that contains leading or trail mmunozjr 3 1,756 Sep-05-2022, 11:56 AM
Last Post: Pedroski55
  "Vlookup" in pandas dataframe doug2019 3 1,858 May-09-2022, 01:35 PM
Last Post: snippsat
  Increase the speed of a python loop over a pandas dataframe mcva 0 1,317 Jan-21-2022, 06:24 PM
Last Post: mcva
  for loop in dataframe in pandas Paulman 7 2,758 Dec-02-2021, 12:15 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020