Format / Suppress Scientific Notation from Python Pandas Aggregation Results

一笑奈何 提交于 2019-11-25 18:56:12

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 
tfhans

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) 

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 

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

See the documentation here.

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:

df['label'].apply(lambda x: '%.17f' % x).values.tolist() 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!