Home > Article > Backend Development > How to Suppress Scientific Notation in Pandas Aggregation Results?
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>
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>
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!