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

后端 未结 3 871
后悔当初
后悔当初 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: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)
    

提交回复
热议问题