How to Suppress Scientific Notation in Pandas Aggregation Results?

DDD
Release: 2024-11-04 07:11:30
Original
293 people have browsed it

How to Suppress Scientific Notation in Pandas Aggregation Results?

Formatting Results to Suppress Scientific Notation in Pandas Aggregation

When aggregating data using Pandas' groupby() function, large numbers may be displayed in scientific notation. To alter this formatting and suppress scientific notation, you can modify the display settings or apply string formatting.

One approach is to use pd.set_option() to define a custom string converter. For example:

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

df1.groupby('dept')['data1'].sum()</code>
Copy after login

This converter will format the output numbers with three decimal places, removing any scientific notation.

Alternatively, you can convert the results to strings and apply string formatting:

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

This will convert the values to strings and apply the %.3f format, also removing scientific notation and adding three decimal places.

While converting numbers to strings may not be ideal for all purposes, it can be useful when you need to customize the formatting for aesthetic reasons, such as displaying numbers with commas or in a specific currency format.

The above is the detailed content of How to Suppress Scientific Notation in Pandas Aggregation Results?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!