2

is there a way to keep scientific notation consistent in txt file and after reading it to pandas dataframe.

pd.read_csv(physio_files[1], sep=" ", header = None)

above command is suppressing scientific notation.

  • 1
    Scientific notation is an external representation of a number, not an internal property. You can display any floating point number in either scientific or fixed-point notation, it is your choice, not pandas'. – DYZ Mar 30 '17 at 23:13
  • @DYZ i agree. But how can i make pandas display in scientific notation instead of default fixed point notation? – learnningprogramming Mar 30 '17 at 23:26
  • This question (especially the first answer) may be helpful http://stackoverflow.com/questions/21137150/format-suppress-scientific-notation-from-python-pandas-aggregation-results – DYZ Mar 30 '17 at 23:35
  • @DYZ the answer shows how to suppress scientific notation exactly opposite to what i intend to – learnningprogramming Mar 30 '17 at 23:40
  • The answer explains how _"you can specify your own string converter..."_ Which could be a converter to the scientific notation. – DYZ Mar 30 '17 at 23:43

1 Answers1

1

Consider the dataframe df

df = pd.DataFrame(1000000., range(5), range(5))
print(df)

           0          1          2          3          4
0  1000000.0  1000000.0  1000000.0  1000000.0  1000000.0
1  1000000.0  1000000.0  1000000.0  1000000.0  1000000.0
2  1000000.0  1000000.0  1000000.0  1000000.0  1000000.0
3  1000000.0  1000000.0  1000000.0  1000000.0  1000000.0
4  1000000.0  1000000.0  1000000.0  1000000.0  1000000.0

You can control the formatting using pandas 'displat.float_format' option. You can also assume a value for that option temporarily using pd.option_context

Use '{:0.4e}'.format as your custom formatter. Change the 4 to suit your needs.

with pd.option_context('display.float_format', '{:0.4e}'.format):
    print(df)

           0          1          2          3          4
0 1.0000e+06 1.0000e+06 1.0000e+06 1.0000e+06 1.0000e+06
1 1.0000e+06 1.0000e+06 1.0000e+06 1.0000e+06 1.0000e+06
2 1.0000e+06 1.0000e+06 1.0000e+06 1.0000e+06 1.0000e+06
3 1.0000e+06 1.0000e+06 1.0000e+06 1.0000e+06 1.0000e+06
4 1.0000e+06 1.0000e+06 1.0000e+06 1.0000e+06 1.0000e+06
piRSquared
  • 285,575
  • 57
  • 475
  • 624