Suppress Scientific Format in a Dataframe Column
Asked Answered
H

3

18

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.

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

Please recommend.

Helmsman answered 18/4, 2018 at 22:5 Comment(0)
I
14

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.

     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.

       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.

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

df
Out[66]: 
       acct_num
0  411889000000
1  987654321000
Incite answered 18/4, 2018 at 22:47 Comment(0)
H
16

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

Use the following instead.

pd.options.display.float_format = '{:.0f}'.format
Hibiscus answered 18/4, 2018 at 22:38 Comment(3)
yes, this works, when I just print df.Accountnumber it displays converted values. However, when I print df.Accountnumber.unique(), it still shows all values in exponential format.Helmsman
In this case, you need to change the data type of account number column , as proposed by @floydn .Hibiscus
you could also change the print format, this solution is better as the original datatype is still validSparklesparkler
I
14

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.

     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.

       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.

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

df
Out[66]: 
       acct_num
0  411889000000
1  987654321000
Incite answered 18/4, 2018 at 22:47 Comment(0)
S
0

#Use this pd.set_option("display.float_format", lambda x: "%.2f" % x)

Suddenly answered 26/10, 2023 at 9:30 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Denisdenise

© 2022 - 2024 — McMap. All rights reserved.