pandas .at versus .loc

后端 未结 4 1412
情歌与酒
情歌与酒 2020-11-27 15:20

I\'ve been exploring how to optimize my code and ran across pandas .at method. Per the documentation

Fast label-based scalar ac

4条回答
  •  醉酒成梦
    2020-11-27 16:07

    Update: df.get_value is deprecated as of version 0.21.0. Using df.at or df.iat is the recommended method going forward.


    df.at can only access a single value at a time.

    df.loc can select multiple rows and/or columns.

    Note that there is also df.get_value, which may be even quicker at accessing single values:

    In [25]: %timeit df.loc[('a', 'A'), ('c', 'C')]
    10000 loops, best of 3: 187 µs per loop
    
    In [26]: %timeit df.at[('a', 'A'), ('c', 'C')]
    100000 loops, best of 3: 8.33 µs per loop
    
    In [35]: %timeit df.get_value(('a', 'A'), ('c', 'C'))
    100000 loops, best of 3: 3.62 µs per loop
    

    Under the hood, df.at[...] calls df.get_value, but it also does some type checking on the keys.

提交回复
热议问题