pandas 抑制数据框列中的科学格式

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

Suppress Scientific Format in a Dataframe Column

pythonpandasdataframescientific-notation

提问by iprof0214

I have a column called accountnumber with values similar to 4.11889000e+11 in a pandas dataframe. I want to suppress the scientific notation and convert the values to 4118890000. I have tried the following method and did not work.

我有一个名为 accountnumber 的列,其值类似于 pandas 数据框中的 4.11889000e+11 。我想抑制科学记数法并将值转换为4118890000。我尝试了以下方法,但没有奏效。

df = pd.read_csv(data.csv)
pd.options.display.float_format = '{:,.3f}'.format

Please recommend.

请推荐。

采纳答案by floydn

I assume the exponential notation for the account numbers must come from the data file. If I create a small csv with the full account numbers, pandas will interpret them as integers.

我假设帐号的指数符号必须来自数据文件。如果我创建一个带有完整帐号的小 csv,pandas 会将它们解释为整数。

     acct_num
0  4118890000
1  9876543210

df['acct_num'].dtype
Out[51]: dtype('int64')

However, if the account numbers in the csv are represented in exponential notation then pandas will read them as floats.

但是,如果 csv 中的帐号以指数表示法表示,那么 Pandas 会将它们读取为浮点数。

       acct_num
0  4.118890e+11
1  9.876543e+11

df['acct_num'].dtype
Out[54]: dtype('float64')

You have 2 options. First, correct the process that creates the csv so the account numbers are written out correctly. The second is to change the data type of the acct_num column to integer.

您有 2 个选择。首先,更正创建 csv 的过程,以便正确写出帐号。二是将acct_num列的数据类型改为整数。

df['acct_num'] = df['acct_num'].astype('int64')

df
Out[66]: 
       acct_num
0  411889000000
1  987654321000

回答by user1319128

You don't need the thousand separators "," and the 3 decimals for the account numbers.

您不需要千位分隔符“,”和帐号的 3 位小数。

Use the following instead.

请改用以下内容。

pd.options.display.float_format = '{:.0f}'.format