Suppressing Scientific Notation in Pandas

Format / Suppress Scientific Notation from Pandas Aggregation Results

Granted, the answer I linked in the comments is not very helpful. You can specify your own string converter like so.

In [25]: pd.set_option('display.float_format', lambda x: '%.3f' % x)

In [28]: Series(np.random.randn(3))*1000000000
Out[28]:
0 -757322420.605
1 -1436160588.997
2 -1235116117.064
dtype: float64

I'm not sure if that's the preferred way to do this, but it works.

Converting numbers to strings purely for aesthetic purposes seems like a bad idea, but if you have a good reason, this is one way:

In [6]: Series(np.random.randn(3)).apply(lambda x: '%.3f' % x)
Out[6]:
0 0.026
1 -0.482
2 -0.694
dtype: object

Suppressing scientific notation in pandas?

Your data is probably object dtype. This is a direct copy/paste of your data. read_csv interprets it as the correct dtype. You should normally only have object dtype on string-like fields.

In [5]: df = read_csv(StringIO(data),sep='\s+')

In [6]: df
Out[6]:
id value
id 1.00 -0.422000
value -0.42 1.000000
percent -0.72 0.100000
played 0.03 -0.043500
money -0.22 0.337000
other NaN NaN
sy -0.03 0.000219
sz -0.33 0.383000

check if your dtypes are object

In [7]: df.dtypes
Out[7]:
id float64
value float64
dtype: object

This converts this frame to object dtype (notice the printing is funny now)

In [8]: df.astype(object)
Out[8]:
id value
id 1 -0.422
value -0.42 1
percent -0.72 0.1
played 0.03 -0.0435
money -0.22 0.337
other NaN NaN
sy -0.03 0.000219
sz -0.33 0.383

This is how to convert it back (astype(float)) also works here

In [9]: df.astype(object).convert_objects()
Out[9]:
id value
id 1.00 -0.422000
value -0.42 1.000000
percent -0.72 0.100000
played 0.03 -0.043500
money -0.22 0.337000
other NaN NaN
sy -0.03 0.000219
sz -0.33 0.383000

This is what an object dtype frame would look like

In [10]: df.astype(object).dtypes
Out[10]:
id object
value object
dtype: object

Suppress scientific notation in Pandas *without* altering precision

Implement np.format_float_positional on a series. If done on df, you will be forced to iterate which can be quite computationally expensive.

Pd.Series

df['b'] =[(lambda x: np.format_float_positional(x))(x) for x in df['b']]

or simply as suggested by @user2357112 supports Monica

df['b'] =[np.format_float_positional(x) for x in df['b']]

Def function
Lets try putting this in def function

import numpy as np

def format_float(df):

cols=list(df.columns)
for col in cols:
df[col]=[np.format_float_positional(x) for x in df[col]]

return df

format_float(df)

outcome

 a          b
0 0.01 0.0000001
1 0.02 0.0000002
2 0.03 0.0000003

Suppress scientific notation in to_markdown() in pandas

I figured out the answer during writing the question.

df.to_markdown() uses tabulate under the hood. We can thus use the floatfmt parameter mentioned in the tabulate readme to disable the formatting:

print(df.to_markdown(floatfmt=''))

yields

|    |           val |
|---:|--------------:|
| 0 | 30000000000.0 |

as @oneextrafact notes in their answer, you can use floatfmt='.0f' to control the number of decimal places shown.

Suppress scientific notation for large numbers in pandas data frame

One simple way to do it is to replace in your code:

plt.bar_label(plot1.containers[0])

with

plt.bar_label(plot1.containers[0], labels=df["visitors"].astype(int))
plt.margins(0.7)

So that the plot looks like this:

Sample Image

Suppress Scientific Format in a Dataframe Column

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

How to suppress scientific notation in values in a pandas dataframe?

You can try something like this:

pd.set_option("float_format", lambda x: f"{x:.2f}")
df
Price
0 770000.00
1 4500000000.00
2 321950.00
3 25000.00
4 476577.00


Related Topics



Leave a reply



Submit