Home > Backend Development > Python Tutorial > How to Format Aggregation Results in Pandas Without Scientific Notation?

How to Format Aggregation Results in Pandas Without Scientific Notation?

Susan Sarandon
Release: 2024-11-02 09:28:31
Original
297 people have browsed it

How to Format Aggregation Results in Pandas Without Scientific Notation?

Format Aggregation Results in Pandas Without Scientific Notation

When performing groupby operations in Pandas, large numbers can be represented in scientific notation. To format these numbers and suppress scientific notation, follow these steps:

Option 1: Customize String Converter

You can specify a custom string converter using the pd.set_option() function. The following code sets the format to display three decimal places:

<code class="python">pd.set_option('display.float_format', lambda x: '%.3f' % x)</code>
Copy after login

Option 2: Convert to String and Format

If you prefer to convert the numbers to strings before formatting, you can use the astype() method followed by the apply() method:

<code class="python">sum_sales_dept.astype(str).apply(lambda x: '%.3f' % float(x))</code>
Copy after login

These options allow you to format aggregation results in Pandas and display large numbers without scientific notation. Note that converting numbers to strings can impact performance and may not be the preferred approach in all cases.

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

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template