Home  >  Article  >  Backend Development  >  How to Suppress Scientific Notation in Pandas Groupby Aggregation Results?

How to Suppress Scientific Notation in Pandas Groupby Aggregation Results?

Patricia Arquette
Patricia ArquetteOriginal
2024-11-03 21:43:02233browse

How to Suppress Scientific Notation in Pandas Groupby Aggregation Results?

Suppress Scientific Notation in Pandas Groupby Aggregation Results

When performing groupby operations in pandas, large numerical results may be displayed in scientific notation. This can be undesirable for readability or subsequent processing.

To modify the formatting, use pd.set_option('display.float_format') to define a custom string converter. For example:

<code class="python">import pandas as pd

pd.set_option('display.float_format', lambda x: '%.3f' % x)</code>

This will display floating-point numbers with three decimal places. Note that this is a global setting and will affect all floating-point numbers displayed in pandas.

While converting numbers to strings for aesthetic purposes is generally discouraged, it can be achieved using apply() with a custom string formatting function:

<code class="python">df['result'].apply(lambda x: '%.3f' % x)</code>

This will apply the specified string formatting to each element in the 'result' column.

The above is the detailed content of How to Suppress Scientific Notation in Pandas Groupby Aggregation Results?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn