Home >Backend Development >Python Tutorial >How Can I Modify Output Formatting for Pandas Groupby Aggregation with Scientific Notation?

How Can I Modify Output Formatting for Pandas Groupby Aggregation with Scientific Notation?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-02 22:53:30828browse

How Can I Modify Output Formatting for Pandas Groupby Aggregation with Scientific Notation?

Modifying Output Formatting for Pandas Groupby Aggregation with Scientific Notation

In pandas, groupby operations can produce scientific notation for large numbers, which can be undesirable.

To modify the format, utilize the display.float_format option to specify a custom string converter. For instance, to add decimals and limit the number of decimal places:

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

This will convert numeric values to strings with three decimal places.

Alternatively, if you prefer not to work with strings, you can apply a lambda function to each value in the Series to customize the formatting:

Series(np.random.randn(3)).apply(lambda x: '%.3f' % x)

The above is the detailed content of How Can I Modify Output Formatting for Pandas Groupby Aggregation with Scientific Notation?. 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