How to trim value to two places after dot without rounding, python

后端 未结 3 832
后悔当初
后悔当初 2021-01-29 02:46

I am searching for fastest method to get each value in my column with only two digits after dot without using round()

pd.Series:

input:

相关标签:
3条回答
  • 2021-01-29 03:19

    Floor div

    s//0.01/100
    0      1.42
    1     12.33
    2    111.66
    3      2.05
    Name: dol, dtype: float64
    
    0 讨论(0)
  • 2021-01-29 03:19

    There are new format specifications, String Format Specification Mini-Language:

    You can do the same as:

    "{0:.2f}".format(1.42345) // output 1.42
    

    Note that the above returns a string. In order to get as float, simply wrap with float(...):

    float("{0:.2f}".format(1.42345)) // output 1.42
    
    0 讨论(0)
  • 2021-01-29 03:21

    One option is to take the floordiv by 0.01 and to divide again the value by 100:

    s.floordiv(0.01).div(100)
    
    0      1.42
    1     12.33
    2    111.66
    3      2.05
    dtype: float64
    

    It clearly performs better than casting to string and extracting up to the second decimal place:

    s = pd.Series(np.random.randn(1_000_000))
    
    %timeit s.astype(str).str.extract(r'(\d+\.\d{2})')
    # 1.76 s ± 42.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
    
    %timeit s.floordiv(0.01).div(100)
    # 42.1 ms ± 3.08 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
    
    %timeit s//0.01/100
    # 40.5 ms ± 3.31 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
    
    0 讨论(0)
提交回复
热议问题