Guides

How to suppress scientific notation in Pandas

July 12, 2019

July 12, 2019

Scientific notation isn't helpful when you are trying to make quick comparisons across your DataFrame, and when your values are not that long. However, Pandas will introduce scientific notation by default when the data type is a float. Here is a way of removing it.

What is Scientific Notation?

Scientific notation (numbers with e) is a way of writing very large or very small numbers. A number is written in scientific notation when a number between 1 and 10 is multiplied by a power of 10.

For example:

  • 2.3e-5, means 2.3 times ten to the minus five power, or 0.000023

  • 4.5e6 means 4.5 times ten to the sixth power, or 4500000 which is the same as 4,500,000

This is a notation standard used by many computer programs including Python Pandas. This is simply a shortcut for entering very large values, or tiny fractions, without using logarithms.

How Scientific Notation Looks in Pandas

Let's create a test DataFrame with random numbers in a float format in order to illustrate scientific notation.

df = pd.DataFrame(np.random.random(5)**10, columns=['random'])

As we can see the random column now contains numbers in scientific notation like 7.413775e-07. If you run the same command it will generate different numbers for you, but they will all be in the scientific notation format. This happens since we are using np.random to generate random numbers.

How to suppress scientific notation in Pandas

There are four ways of showing all of the decimals when using Python Pandas instead of scientific notation.

Solution 1: use .round()

df.round(5)

Solution 2: Use apply to change format

df.apply(lambda x: '%.5f' % x, axis=1)

Solution 3: Use .set_option()

Note that .set_option() changes behavior globaly in Jupyter Notebooks, so it is not a temporary fix.

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

In order to revert Pandas behaviour to defaul use .reset_option().

pd.reset_option('display.float_format')

Note that the DataFrame was generated again using the random command, so we now have different numbers in it.

Solution 4: Assign display.float_format

You can change the display format using any Python formatter:

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

To revert back, you can use pd.reset_option with a regex to reset more than one simultaneously. In this case to reset all options starting with display you can:

pd.reset_option('^display.', silent=True)

Now that you know how to modify the default Pandas output and how to suppress scientific notation, you are more empowered.

Subscribe

Get fresh web design stories, tips, and resources delivered straight to your inbox every week.

Get fresh web design stories, tips, and resources delivered straight to your inbox every week.

Continue Reading

Apps

Timestripe - my new favourite productivity app

March 5, 2023

Guides

How to scrape tables from websites using Pandas read_html() function

February 2, 2023

Guides

Drop all duplicate rows across multiple columns in Python Pandas

January 28, 2023

Guides

How to create effective prompts for AI image generation

August 15, 2022

Guides

Generate Huge Datasets With Fake Data Easily and Quickly using Python and Faker

April 16, 2022

Guides

How to change or update a specific cell in Python Pandas Dataframe

March 25, 2021

Guides

How to add a row at the top in Pandas dataframe

March 22, 2021

Guides

Creating WordClouds in Python from a single-column in Pandas dataframe

November 15, 2020

Guides

Python Regex examples - How to use Regex with Pandas

September 9, 2020

Guides

Python regular expressions (RegEx) simple yet complete guide for beginners

September 15, 2020

Guides

8 Python Pandas Value_counts() tricks that make your work more efficient

May 31, 2020

Guides

Exploring Correlation in Python: Pandas, SciPy

May 5, 2020

Guides

How to add new columns to Pandas dataframe?

March 22, 2020

Guides

Delete column/row from a Pandas dataframe using .drop() method

February 2, 2020

Guides

How to visualize data with Matplotlib from a Pandas Dataframe

November 15, 2019

Guides

The ultimate beginners guide to Group by in Python Pandas

August 8, 2019

Guides

Guide to renaming columns with Python Pandas

July 2, 2019

Guides

The complete beginners guide to Pandas

June 29, 2019

Guides

Data project #1: Stockmarket analysis

June 29, 2019

Blue and red light digital wallpaper
Blue and red light digital wallpaper

Guides

Use Jupyter notebooks anywhere

June 10, 2019