如何摆脱大 pandas 将Excel工作表中的大量数字转换为指数? [英] how to get rid of pandas converting large numbers in excel sheet to exponential?

查看:99
本文介绍了如何摆脱大 pandas 将Excel工作表中的大量数字转换为指数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在excel工作表中,我有两列带有大数字.

In the excel sheet , i have two columns with large numbers.

但是当我使用read_excel()读取excel文件并显示数据框时,

But when i read the excel file with read_excel() and display the dataframe,

这两列以科学格式以指数形式打印.

those two columns are printed in scientific format with exponential.

如何摆脱这种格式?

谢谢

熊猫输出

推荐答案

通过熊猫的选项控制科学计数法的应用方式:

The way scientific notation is applied is controled via pandas' options:

import pandas as pd
pd.set_option('display.precision',3)

pd.DataFrame({'x':[.001]})   

        x
0   0.001

但是

pd.DataFrame({'x':[.0001]})

            x
0   1.000e-04

但是

pd.set_option('display.precision',4)
pd.DataFrame({'x':[.0001]})

         x
0   0.0001

您可能会在选项和设置中看到有关如何控制熊猫输出的更多信息. a>熊猫文档部分.

You may see more about how to control pandas output in Options and Settings section of pandas docs.

编辑

如果这仅仅是出于演示目的,则可以转换您的 数据转换为字符串,同时按列对其进行格式化:

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

这篇关于如何摆脱大 pandas 将Excel工作表中的大量数字转换为指数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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