格式化/抑制 Python Pandas 聚合结果中的科学记数法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21137150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 22:09:11  来源:igfitidea点击:

Format / Suppress Scientific Notation from Python Pandas Aggregation Results

pythonpandasfloating-pointscientific-notationnumber-formatting

提问by horatio1701d

How can one modify the format for the output from a groupby operation in pandas that produces scientific notation for very large numbers?

如何修改 Pandas 中 groupby 操作的输出格式,该操作为非常大的数字生成科学记数法?

I know how to do string formatting in python but I'm at a loss when it comes to applying it here.

我知道如何在 python 中进行字符串格式化,但是在这里应用它时我不知所措。

df1.groupby('dept')['data1'].sum()

dept
value1       1.192433e+08
value2       1.293066e+08
value3       1.077142e+08

This suppresses the scientific notation if I convert to string but now I'm just wondering how to string format and add decimals.

如果我转换为字符串,这会抑制科学记数法,但现在我只是想知道如何设置字符串格式并添加小数。

sum_sales_dept.astype(str)

采纳答案by Dan Allan

Granted, the answer I linked in the comments is not very helpful. You can specify your own string converter like so.

当然,我在评论中链接的答案并不是很有帮助。您可以像这样指定自己的字符串转换器。

In [25]: pd.set_option('display.float_format', lambda x: '%.3f' % x)

In [28]: Series(np.random.randn(3))*1000000000
Out[28]: 
0    -757322420.605
1   -1436160588.997
2   -1235116117.064
dtype: float64

I'm not sure if that's the preferred way to do this, but it works.

我不确定这是否是执行此操作的首选方法,但它有效。

Converting numbers to strings purely for aesthetic purposes seems like a bad idea, but if you have a good reason, this is one way:

纯粹出于审美目的将数字转换为字符串似乎是一个坏主意,但如果您有充分的理由,这是一种方法:

In [6]: Series(np.random.randn(3)).apply(lambda x: '%.3f' % x)
Out[6]: 
0     0.026
1    -0.482
2    -0.694
dtype: object

回答by tfhans

Here is another way of doing it, similar to Dan Allan's answerbut without the lambda function:

这是另一种方法,类似于Dan Allan 的答案,但没有 lambda 函数:

>>> pd.options.display.float_format = '{:.2f}'.format
>>> Series(np.random.randn(3))
0    0.41
1    0.99
2    0.10

or

或者

>>> pd.set_option('display.float_format', '{:.2f}'.format)

回答by evil242

If you would like to use the values, say as part of csvfile csv.writer, the numbers can be formatted before creating a list:

如果您想使用这些值,例如作为 csvfile csv.writer 的一部分,可以在创建列表之前对数字进行格式化:

df['label'].apply(lambda x: '%.17f' % x).values.tolist()

回答by Vlad Bezden

You can use round function just to suppress scientific notation for specific dataframe:

您可以使用 round 函数来抑制特定数据帧的科学记数法:

df1.round(4)

or you can suppress is globally by:

或者您可以通过以下方式全局抑制:

pd.options.display.float_format = '{:.4f}'.format

回答by florestan

If you want to style the output of a data frame in a jupyter notebook cell, you can set the display style on a per-dataframe basis:

如果要在 jupyter notebook 单元格中设置数据框输出的样式,可以在每个数据框的基础上设置显示样式:

df = pd.DataFrame({'A': np.random.randn(4)*1e7})
df.style.format("{:.1f}")

enter image description here

在此处输入图片说明

See the documentation here.

请参阅此处的文档。