问题:从Python熊猫聚合结果格式化/抑制科学计数法

如何对熊猫的groupby运算输出的格式进行修改,从而产生大量的科学计数法?

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

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

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

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

sum_sales_dept.astype(str)

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

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

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)

回答 0

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

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

我不确定这是否是首选的方法,但是可以。

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

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

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

回答 1

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

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

要么

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

Here is another way of doing it, similar to Dan Allan’s answer but without the lambda function:

>>> 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)

回答 2

您可以使用舍入功能只是为了抑制特定数据框的科学计数法:

df1.round(4)

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

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

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

df1.round(4)

or you can suppress is globally by:

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

回答 3

如果要在jupyter笔记本单元格中设置数据框输出的样式,则可以基于每个数据框设置显示样式:

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

在此处输入图片说明

请参阅此处的文档。

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:

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

enter image description here

See the documentation here.


回答 4

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

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

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

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

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。