🚀Pandas features: use at and iat in loops instead of iloc and loc
The Python-library Pandas has iloc and loc functions to access dateframe values by row index and index or column name. But running them inside loops is time consuming. If you replace loc with at, or iloc with iat, the execution time of a for loop can be reduced by a factor of 60!
Such a different difference in speed is due to the nature of these functions: at and iat include access to a scalar, that is, to one element of the dataframe. And loc and iloc are used for simultaneous access to elements (rows, dataframes), i.e. they are initially needed to perform vectorized operations.
Because of at/iat are used for accessing the scalar environment, they are faster than loc/iloc for serie/dataframe access and take more space and time. Therefore, using loc/iloc inside loops in Python is slow and should be replaced with at/iat, which exucute faster. However, loc and iloc work fine outside of Python loops for vectorized operations.
https://medium.com/codex/dont-use-loc-iloc-with-loops-in-python-instead-use-this-f9243289dde7
Post #498
582