2

I use an API that returns a json string that I write to a csv file using pandas - to_csv. Some of the fields are strings and some are numeric. One of the fields is a long serial number written to csv in a scientific way and therefore its recent digits appear as zeros, for example: json - 7052770924403150081, csv - 7.05277E+18, reading this csv in another python program - 7052770924403150000

How can I keep this number in the csv file as without any change? Of course without damaging the other fields in the file?

I tried float_format but it did not help: df.to_csv (self.csv_video, float_format = '%f')

Would appreciate help.

ofer bin
  • 33
  • 2
  • Ensure the column is parsed as an integer or a string, not as a floating-point number. – AKX Apr 11 '22 at 07:29
  • why are you using pandas to create your csv? – juanpa.arrivillaga Apr 11 '22 at 07:29
  • 2
    Does this answer your question? [how to get rid of pandas converting large numbers in excel sheet to exponential?](https://stackoverflow.com/questions/38689125/how-to-get-rid-of-pandas-converting-large-numbers-in-excel-sheet-to-exponential) – Tranbi Apr 11 '22 at 07:29
  • Did you try adding `pd.set_option('display.float_format', lambda x: '%f' % x)` before your export? – PirateNinja Apr 11 '22 at 07:30
  • @juanpa.arrivillaga That's also a good question, of course. `csv` would likely do fine here... – AKX Apr 11 '22 at 07:30
  • @AKX not only would it do fine, it would straight-forwardly preserve the exact digits in the large integer, since it isn't being converted to a `numpy` numeric type, in this case, a `np.float64` I presume, in which case, it loses that information. – juanpa.arrivillaga Apr 11 '22 at 07:31
  • This is a code that a former co-worker wrote and I just need to update it. Prefers not to make massive changes but only spot as much as possible. – ofer bin Apr 11 '22 at 07:52
  • 1
    @oferbin You would need to show the relevant code, then. – AKX Apr 11 '22 at 12:20

0 Answers0