Python 抑制熊猫中的科学记数法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17737300/
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-19 09:03:06  来源:igfitidea点击:

Suppressing scientific notation in pandas?

pythonnumpypandas

提问by user1244215

I have a DataFrame in pandas where some of the numbers are expressed in scientific notation (or exponent notation) like this:

我在 Pandas 中有一个 DataFrame,其中一些数字用科学记数法(或指数记数法)表示,如下所示:

                  id        value
id              1.00    -4.22e-01
value          -0.42     1.00e+00
percent        -0.72     1.00e-01
played          0.03    -4.35e-02
money          -0.22     3.37e-01
other            NaN          NaN
sy             -0.03     2.19e-04
sz             -0.33     3.83e-01

And the scientific notation makes what should be an easy comparison, needlessly difficult. I assume it's the 21900 value that's screwing it up for the others. I mean 1.0 is encoded. ONE!

科学记数法使本应很容易的比较变得不必要地困难。我认为是 21900 的值让其他人搞砸了。我的意思是 1.0 已编码。一!

This doesn't work:

这不起作用:

np.set_printoptions(supress=True) 

And pandas.set_printoptionsdoesn't implement suppress either, and I've looked all at pd.describe_options()in despair, and pd.core.format.set_eng_float_format()only seems to turn it on for all the other float values, with no ability to turn it off.

并且pandas.set_printoptions也没有实现抑制,我pd.describe_options()绝望地看着所有内容,pd.core.format.set_eng_float_format()似乎只为所有其他浮点值打开它,无法关闭它。

采纳答案by Jeff

Your data is probably objectdtype. This is a direct copy/paste of your data. read_csvinterprets it as the correct dtype. You should normally only have objectdtype on string-like fields.

您的数据可能是objectdtype。这是您数据的直接复制/粘贴。read_csv将其解释为正确的 dtype。您通常应该只object在类似字符串的字段上使用dtype。

In [5]: df = read_csv(StringIO(data),sep='\s+')

In [6]: df
Out[6]: 
           id     value
id       1.00 -0.422000
value   -0.42  1.000000
percent -0.72  0.100000
played   0.03 -0.043500
money   -0.22  0.337000
other     NaN       NaN
sy      -0.03  0.000219
sz      -0.33  0.383000

check if your dtypes are object

检查您的 dtypes 是否是 object

In [7]: df.dtypes
Out[7]: 
id       float64
value    float64
dtype: object

This converts this frame to objectdtype (notice the printing is funny now)

这会将这个框架转换为objectdtype(注意现在打印很有趣)

In [8]: df.astype(object)
Out[8]: 
           id     value
id          1    -0.422
value   -0.42         1
percent -0.72       0.1
played   0.03   -0.0435
money   -0.22     0.337
other     NaN       NaN
sy      -0.03  0.000219
sz      -0.33     0.383

This is how to convert it back (astype(float)) also works here

这是如何将它转换回来 ( astype(float)) 也适用于这里

In [9]: df.astype(object).convert_objects()
Out[9]: 
           id     value
id       1.00 -0.422000
value   -0.42  1.000000
percent -0.72  0.100000
played   0.03 -0.043500
money   -0.22  0.337000
other     NaN       NaN
sy      -0.03  0.000219
sz      -0.33  0.383000

This is what an objectdtype frame would look like

这就是objectdtype 框架的样子

In [10]: df.astype(object).dtypes
Out[10]: 
id       object
value    object
dtype: object

回答by citynorman

quick temporary: df.round(4)

快速临时: df.round(4)

global: pd.options.display.float_format = '{:20,.2f}'.format

全球的: pd.options.display.float_format = '{:20,.2f}'.format

回答by evil242

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

如果您想将值用作列表中的格式化字符串,例如作为 csvfile csv.writier 的一部分,则可以在创建列表之前对数字进行格式化:

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