Specify float_format differently for each column (scientific notation vs decimal precision)

依然范特西╮ 提交于 2021-02-05 09:34:07

问题


I have multiple columns, some I want in scientific notation, others to a specific level of decimal precision:

   Frequency    n
0     0.0023  2.3
1     0.0420  4.5
2     0.5460  6.7
3     0.1230  8.9

Frequency can have small orders of magnitude, so I end up with a billion zeros. The 'n' column should have a set number of decimal places.

I've attempted to do the following on the 'Frequency' column:

fntables['Frequency'].options.display.float_format = '{:.2e}'.format

This returns the expected error that "'Series' object has no attribute 'options'" as the options are on the pandas-level instance.


回答1:


  • pandas: Available options

Sample data

import pandas as pd
import numpy as np

# sample data
np.random.seed(10)
df = pd.DataFrame(np.random.random(5)**10, columns=['A'])

# display(df)
              A
0  7.453316e-02
1  1.481116e-17
2  1.043476e-02
3  5.542183e-02
4  9.477913e-04

set precision: Global setting

  • Floating point output precision in terms of number of places after the decimal, for regular formatting as well as scientific notation. Similar to numpy’s precision print option
  • pd.reset_option('precision') resets the setting.
pd.set_option('precision', 3)

# display(df)
           A
0  7.453e-02
1  1.481e-17
2  1.043e-02
3  5.542e-02
4  9.478e-04

print(df.iloc[0, 0])
[out]:
0.07453316227023182

set float_format: Global setting

  • The callable should accept a floating point number and return a string with the desired format of the number. This is used in some places like SeriesFormatter. See core.format.EngFormatter for an example.
  • pd.reset_option('float_format') resets the setting.
pd.options.display.float_format = '{:.3e}'.format

# display(df)
          A
0 7.453e-02
1 1.481e-17
2 1.043e-02
3 5.542e-02
4 9.478e-04

print(df.iloc[0, 0])
[out]:
0.07453316227023182

apply or map: str format

df['format'] = df.A.apply(lambda x: f'{x:0.3e}')
df['format'] = df.A.apply('{:,.3e}'.format)
df['format'] = df.A.map('{:,.3e}'.format)

# display(df)
              A     format
0  7.453316e-02  7.453e-02
1  1.481116e-17  1.481e-17
2  1.043476e-02  1.043e-02
3  5.542183e-02  5.542e-02
4  9.477913e-04  9.478e-04

np.format_float_scientific: str format

df['numpy_format'] = df.A.map(lambda x: np.format_float_scientific(x, precision = 3))

# display(df)
              A numpy_format
0  7.453316e-02    7.453e-02
1  1.481116e-17    1.481e-17
2  1.043476e-02    1.043e-02
3  5.542183e-02    5.542e-02
4  9.477913e-04    9.478e-04



回答2:


Ended up using list comprehension with the np.format_float_scientific method

fntables['A_sci'] = [np.format_float_scientific(x, precision = 3) for x in fntables['A']]


来源:https://stackoverflow.com/questions/63604302/specify-float-format-differently-for-each-column-scientific-notation-vs-decimal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!