Suppressing scientific notation in pandas?

后端 未结 4 1347
清酒与你
清酒与你 2020-11-27 05:49

I have a DataFrame in pandas where some of the numbers are expressed in scientific notation (or exponent notation) like this:

                  id        val         


        
相关标签:
4条回答
  • 2020-11-27 06:33

    If you would like to use the values as formated string in a list, say as part of csvfile csv.writier, the numbers can be formated before creating a list:

    df['label'].apply(lambda x: '%.17f' % x).values.tolist()
    
    0 讨论(0)
  • 2020-11-27 06:44

    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
    
    0 讨论(0)
  • 2020-11-27 06:49

    quick temporary: df.round(4)

    global: pd.options.display.float_format = '{:20,.2f}'.format

    0 讨论(0)
  • 2020-11-27 06:55

    Try this which will give you scientific notation only for large and very small values (and adds a thousands separator unless you omit the ","):

    pd.set_option('display.float_format', lambda x: '%,g' % x)
    

    Or to almost completely suppress scientific notation without losing precision, try this:

    pd.set_option('display.float_format', str)
    
    0 讨论(0)
提交回复
热议问题