在 pandas 中使用科学记数法 [英] Using scientific notation in pandas

查看:465
本文介绍了在 pandas 中使用科学记数法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在熊猫中找到了很多关于如何抑制科学记谱法的答案,但是我怎样才能启用它呢?我发现选项 pd.set_option('precision',2),但它不会将大数转换为科学记数法。



例如,我希望数字 123066.14 被格式化为 1.23E + 5 。我正在使用一个 pandas.DataFrame ,在导出/打印时设置整个列的格式会很有用。

解决方案

OK,我想通了这一点,你可以使用的 set_option 和传递格式字符串选项 'display.float_format'

  In [76]:
pd.set_option('display.float_format', '{:.2g}'。格式)

在[78]中:
pd.Series(data = [0.00000001])$ ​​b
$ b输出[78]:
0 1e-08
dtype:float64



$ p $匹配您所需的输出:

  In [79 ]:
pd.set_option( 'display.float_format', '{:.2E}' 格式)
pd.Series(数据= [0.00000001])$ ​​b
$ b OUT [ 79]:
0 1.00E-08
dtype:float64


I found plenty of answers on how to suppress scientific notation in pandas, but how do I enable it? I found the option pd.set_option('precision', 2) but it does not turn large numbers into scientific notation.

For example, I would like the number 123066.14 to be formatted as 1.23E+5. I am using a pandas.DataFrame, and it would be useful to set the formatting for an entire column when exporting/printing.

解决方案

OK, I figured this out, you can use set_option and pass a format string to option 'display.float_format':

In [76]:
pd.set_option('display.float_format', '{:.2g}'.format)

In [78]:
pd.Series(data=[0.00000001])

Out[78]:
0   1e-08
dtype: float64

EDIT

to match your desired output:

In [79]:
pd.set_option('display.float_format', '{:.2E}'.format)
pd.Series(data=[0.00000001])

Out[79]:
0   1.00E-08
dtype: float64

这篇关于在 pandas 中使用科学记数法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆