Home > database >  How do I show small numbers with scientific notation in Pandas Dataframe Python?
How do I show small numbers with scientific notation in Pandas Dataframe Python?

Time:12-11

I'm working with a dataframe that on one column has numbers of different order of magnitude, the biggest is 0.315 and the smallest is 0.xxe-28. My problem is that when I display the pd dataframe I get this:

enter image description here

Small numbers are rounded to 0.0

I'd like to show every number in scientific notation like: 3.15e-1, 6.55e-28, and so on. How can I do that? Thank you!

EDIT: I found that pd.set_option('display.float_format', '{:.2E}'.format) almost does the trick. The problem is that now all the columns are in that format:

enter image description here

Is it possible to use set_option to specify a format just for one column?

CodePudding user response:

You can either set the format for the whole dataframe with :

pd.options.display.float_format = '{:12.5e}'.format

Or set the format for each column individualy with :

output = df.to_string(formatters={
    'Coefficient': '{:12.4f}'.format,
    'P-value': '{:12.5e}'.format
})
print(output)
  • Related