chowpay
chowpay

Reputation: 1687

pandas converting a float remove exponents

I have data that looks like this:

Date            MBs     GBs
0   2018-08-14 20:10    32.00 MB    0.00 GB
1   2018-08-14 20:05    4.00 MB     0.00 GB
2   2018-08-14 20:00    1000.99 MB  1.23 GB

I stripped away the MB and GB by doing this:

df['MBs']=df['MB'].str.strip('MB')
df['GBs']=df['GB'].str.strip('GB')

Then converted the number to a float and got the totals:

df['MBs'] = df['MBs'].astype('float')
df['GBs'] = df['MBs'].astype('float')

df.loc['Total', ['MBs', 'GBs']] = df.sum()

But when I run it my data has exponents

Date    Data Transferred (MB)   Data Transferred (GB)
146 2018-08-14 08:00:00 1.871237e+05    1.874017e+05
147 2018-08-14 07:55:00 1.123211e+05    1.961854e+05
148 2018-08-14 07:50:00 2.187703e+05    2.187123e+05
...
Total       1.408910e+08    1.408910e+08

How do I convert change that float from exponent to "normal", im only converting it because I need to get the totals

Upvotes: 5

Views: 8834

Answers (2)

runzhi xiao
runzhi xiao

Reputation: 184

You are trying to avoid using scientific notation:So here is what you can do:

import pandas as pd
pd.set_option('display.float_format', lambda x: '%.3f' % x)

this line of code set the pandas display format so it will not use scientific notaion

reference:http://pandas.pydata.org/pandas-docs/stable/options.html?highlight=display%20float_format

Upvotes: 9

cs95
cs95

Reputation: 402563

That's just how floats are represented by pandas, and that isn't something you change. You can, however, change the representation if you format the data as a string.

# Don't run this line.
# df = pd.concat([df] * 10000, ignore_index=True) 
# This should be run on the *unstripped* version of your DataFrame.
df.loc['Total', ['MBs', 'GBs']] = (
    df[['MBs', 'GBs']]
       .stack()
       .str.split()
       .str[0]
       .astype(float)
       .unstack()
       .sum()
       .agg('{:.2f}'.format))

df.tail()

                   Date          MBs       GBs
29996  2018-08-14 20:00   1000.99 MB   1.23 GB
29997  2018-08-14 20:10     32.00 MB   0.00 GB
29998  2018-08-14 20:05      4.00 MB   0.00 GB
29999  2018-08-14 20:00   1000.99 MB   1.23 GB
Total               NaN  10369900.00  12300.00

Upvotes: 2

Related Questions