抑制大 pandas 的科学计数法吗? [英] Suppressing scientific notation in pandas?

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

问题描述

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

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

                  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

科学的记法使得应该进行简单的比较,而不必要地变得困难.我认为正是21900的价值将其推向了其他水平.我的意思是1.0被编码.一!

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!

这不起作用:

np.set_printoptions(supress=True) 

pandas.set_printoptions都没有实现抑制,并且我在绝望中查看了pd.describe_options(),而pd.core.format.set_eng_float_format()似乎仅针对所有其他浮点值将其打开,而无法将其打开关闭.

And pandas.set_printoptions doesn'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.

推荐答案

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

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

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

检查您的dtype是否为object

check if your dtypes are object

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

这会将此帧转换为object dtype(注意现在打印很有趣)

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

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

这是将其转换回(astype(float))的方法,在这里也可以使用

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

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

这是object dtype框架的外观

This is what an object dtype frame would look like

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

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

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