How to Suppress Scientific Notation in Pandas

Scientific notation is a way of expressing numbers using exponents, which can be useful for very large or very small numbers, but can also make data difficult to read and interpret. Fortunately, Pandas provides a way to suppress scientific notation and display numbers in standard decimal format.

Here are some ways to suppress scientific notation in Pandas:

Using pandas.set_option():

You can use the set_option() method to set the display format of pandas to display numbers in standard decimal format instead of scientific notation. Here is an example code:

import pandas as pd

pd.set_option('display.float_format', lambda x: '%.3f' % x)

data = pd.DataFrame({'col1': [123456789, 0.0000000123456], 'col2': [987654321, 0.0000000098765]})

print(data)

This will display the data in the data DataFrame without scientific notation:

col1 col2
0 123456789.000 987654321.000
1 0.000 0.000

Using pandas.DataFrame.round():

You can also use the round() method to round the values in your DataFrame to a certain number of decimal places. Here is an example code:

import pandas as pd

data = pd.DataFrame({'col1': [123456789, 0.0000000123456], 'col2': [987654321, 0.0000000098765]})

data = data.round(3)

print(data)

This will display the data in the data DataFrame without scientific notation:

col1 col2
0 123456789.000 987654321.000
1 0.000 0.000

Using pandas.DataFrame.apply():

You can also use the apply() method to apply a formatting function to all the values in your DataFrame. Here is an example code:

import pandas as pd

data = pd.DataFrame({'col1': [123456789, 0.0000000123456], 'col2': [987654321, 0.0000000098765]})

def format_func(x):
return '{:.3f}'.format(x)

data = data.applymap(format_func)

print(data)

This will display the data in the data DataFrame without scientific notation:

col1 col2
0 123456789.000 987654321.000
1 0.000 0.000

Using pandas.options.display.float_format:

You can also use the pandas.options.display.float_format attribute to set the default formatting for floats. Here is an example code:

import pandas as pd

pd.options.display.float_format = '{:.3f}'.format

data = pd.DataFrame({'col1': [123456789, 0.0000000123456], 'col2': [987654321, 0.0000000098765]})

print(data)

This will display the data in the data DataFrame without scientific notation:

col1 col2
0 123456789.000 987654321.000
1 0.000 0.000

Note that using the pd.options.display.float_format attribute will affect the formatting of all floats in your code, not just the ones in your DataFrame.

Avoiding Unnecessary Suppression of Scientific Notation

To prevent excessive suppression of scientific notation:

  • Use pandas.set_option() for global float formatting control.
  • Apply rounding with pandas.DataFrame.round() or .apply() for small numbers.
  • Check format support when exporting data.
  • Verify proper handling of scientific notation in Pandas-based visualizations.

See also:
Disable scientific notation in Excel

Leave a Reply