Python 如何摆脱将excel表中的大数字转换为指数的熊猫?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38689125/
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
how to get rid of pandas converting large numbers in excel sheet to exponential?
提问by Nathaniel Babalola
In the excel sheet , i have two columns with large numbers.
在excel表中,我有两列大数字。
But when i read the excel file with read_excel() and display the dataframe,
但是当我用 read_excel() 读取 excel 文件并显示数据框时,
those two columns are printed in scientific format with exponential.
这两列以指数形式以科学格式打印。
How can get rid of this format?
如何摆脱这种格式?
Thanks
谢谢
Output in Pandas
熊猫输出
回答by Sergey Bushmanov
The way scientific notation is applied is controled via pandas' options:
应用科学记数法的方式是通过 pandas 的选项控制的:
import pandas as pd
pd.set_option('display.precision',3)
pd.DataFrame({'x':[.001]})
x
0 0.001
but
但
pd.DataFrame({'x':[.0001]})
x
0 1.000e-04
but
但
pd.set_option('display.precision',4)
pd.DataFrame({'x':[.0001]})
x
0 0.0001
You may see more about how to control pandas output in Options and Settingssection of pandas docs.
您可能会在Pandas 文档的选项和设置部分看到更多关于如何控制 Pandas 输出的信息。
EDIT
编辑
If this is simply for presentational purposes, you may convert your data to strings while formatting them on a column-by-column basis:
如果这只是出于演示目的,您可以将数据转换为字符串,同时逐列格式化它们:
df = pd.DataFrame({'Traded Value':[67867869890077.96,78973434444543.44],
'Deals':[789797, 789878]})
df
Deals Traded Value
0 789797 6.786787e+13
1 789878 7.897343e+13
df['Deals'] = df['Deals'].apply(lambda x: '{:d}'.format(x))
df['Traded Value'] = df['Traded Value'].apply(lambda x: '{:.2f}'.format(x))
df
Deals Traded Value
0 789797 67867869890077.96
1 789878 78973434444543.44
An alternative more straightforward method would to put the following line at the top of your code that would format floats only:
另一种更直接的方法是将以下行放在仅格式化浮点数的代码顶部:
pd.options.display.float_format = '{:.2f}'.format