0

I've read about how you can write a df to CSV in pandas, and suppress scientific notation using:

float_format='{:f}'

But what about an existing csv with several columns that look like this:

FIPS_BLOCK  FIPS_BLKGR  FIPS_TRACT
5.51E+14    5.51E+11    5.51E+10
5.51E+14    5.51E+11    5.51E+10
5.51E+14    5.51E+11    5.51E+10
5.51E+14    5.51E+11    5.51E+10

Is there any way to re-write this csv and change these columns to not be scientific notation? I want them to be eventually strings (in other words, they are numeric, but want them to be text). I think I need to pass the CSV in pandas, do something, then write it again (overwrite existing CSV).

halfer
  • 19,824
  • 17
  • 99
  • 186
DiamondJoe12
  • 1,879
  • 7
  • 33
  • 81

1 Answers1

0

The solution to this question uses pandas' built in to_numeric function to cast entries with scientific notation:

df1 = df.apply(pd.to_numeric, args=('coerce',))

All credit for this solution goes to the author of that answer, @Anton Protopopov

dsillman2000
  • 976
  • 1
  • 8
  • 20
  • But some of my fields are not numeric (i.e. string.) I only want to get numeric for those specific fields which are in scientific notation. – DiamondJoe12 Oct 19 '21 at 15:52
  • In that case, I recommend indexing only those columns. So store a list of the numeric columns, and you can say `df[numeric_cols] = df[numeric_cols].apply(pd.to_numeric, args=('coerce',))` – dsillman2000 Oct 19 '21 at 16:27
  • I tried this. It's still outputting in scientific notation and I'm pulling my hair out. – DiamondJoe12 Oct 19 '21 at 16:32