Member-only story
Don’t use loc/iloc with Loops In Python, Instead, Use This!
Run your loops at a 60X faster speed
Recently, I was experimenting with loops in Python and realized that using ‘iloc’/ ‘loc’ within the loops takes a lot of time to execute. The immediate next question was why is ‘loc’ taking too much time and what is the alternative to ‘loc’?
In this blog, we will answer these questions by looking at some practical examples.
What is loc — if you don’t know already!
The loc[] function is a pandas function that is used to access the values within a DataFrame using the row index and column name. It is used when you know which row and column you want to access.
Let’s understand loc using an example. We have the following pandas DataFrame named df(shown below) and we want to access the value corresponding to the 2nd row in the column ‘a’ i.e. 10.
We can access the value using the following code:
##df.loc[index, column_name]
df.loc[1,'a']
### Output: 10