with pandas, you’ve most likely came upon this traditional confusion: do you have to use loc or iloc to extract information? At first look, they give the impression of being virtually an identical. Each are used to slice, filter, and retrieve rows or columns from a DataFrame — but one tiny distinction in how they work can utterly change your outcomes (or throw an error that leaves you scratching your head).
I keep in mind the primary time I attempted choosing a row with df.loc[0] and questioned why it didn’t work. The explanation? Pandas doesn’t at all times “suppose” by way of positions — generally it makes use of labels. That’s the place the loc vs iloc distinction is available in.
On this article, I’ll stroll by means of a easy mini mission utilizing a small pupil efficiency dataset. By the tip, you’ll not solely perceive the distinction between loc and iloc, but in addition know precisely when to make use of every in your personal information evaluation.
Introducing the dataset
The dataset comes from ChatGPT. It incorporates some fundamental pupil examination rating information. Right here’s a snapshot of our dataset
import pandas as pd
df = pd.read_csv(‘student_scores.csv’)
df
Output:

I’ll attempt to carry out some information extraction duties utilizing loc and iloc, like
- Extracting a single row from the DataFrame
- Extracting a single worth
- Extracting a number of rows
- Slicing a spread of rows
- Extracting particular columns and
Boolean Filtering
First, let me briefly clarify what loc and iloc are in Pandas.
What’s loc and iloc
Loc and iloc are information extraction strategies in Pandas. They’re fairly useful for choosing information from information.
Loc makes use of labels to retrieve information from a DataFrame, so I discover it simpler to make use of. Iloc, nevertheless, are useful for a extra exact retrieval of information, as a result of iloc selects information primarily based on the integer positions of the rows and columns, much like how you’d index a Python record or array.
However for those who’re like me, you could be questioning. If loc is clearly simpler due to row labels, why hassle utilizing iloc? Why hassle making an attempt to determine row indexes, particularly for those who’re coping with massive datasets? Listed below are a few causes.
- Loads of instances, datasets don’t include neat row indexes (like 101, 102, …). As an alternative, you’ve gotten a plain index (
0, 1, 2, …), otherwise you would possibly misspell row labelling when retrieving information. On this case, you’re higher off utilizing iloc. Later on this article, it’s one thing we’ll be addressing additionally. - In some situations, like machine studying preprocessing, labels don’t actually matter. You solely care a few snapshot of the info. As an illustration, the primary or final three information. iloc is de facto useful on this situation.
ilocmakes the code shorter and fewer fragile, particularly if labels change, which may break your machine studying mannequin - Loads of datasets have duplicate row labels. On this case,
ilocat all times works since positions are distinctive. - The underside line is, use loc when your dataset has clear, significant labels and also you need your code to be readable.
- Use iloc whenever you want position-based management, or when labels are lacking/messy.
Now that I’ve cleared the air, right here’s the fundamental syntax for loc and iloc beneath:
df.loc[rows, columns]
df.iloc[rows, columns]
The syntax is just about the identical. With this syntax, let’s attempt to retrieve some information utilizing loc and iloc.
Extracting a single row from the DataFrame
To make a correct demonstration, let’s first change the column index and make it student_id. At the moment, pandas is auto-indexing:
# setting student_id as index
df.set_index('student_id', inplace=True)
Right here’s the output:

Appears higher. Now, let’s attempt to retrieve all of Bob’s information. Right here’s tips on how to method that utilizing loc:
df.loc[102]
All I’m doing right here is specifying the row label. This could retrieve all of Bob’s information.
Right here’s the output:
title Bob
math 58
english 64
science 70
Identify: 102, dtype: object
The cool factor about that is that I can drill down, kinda like a hierarchy. As an illustration, let’s attempt to retrieve particular data about Bob, like his rating on math.
df.loc[102, ‘math’]
The output can be 58.
Now let’s do this utilizing iloc. If you happen to’re accustomed to lists and arrays, indexing at all times begins at 0. So if I need to retrieve the primary report within the DataFrame, I’ll should specify the index 0. On this case, I’m making an attempt to retrieve Bob, which is the second row in our DataFrame — so, on this case, the index can be 1.
df.iloc[1]
We’d get the identical output as above:
title Bob
math 58
english 64
science 70
Identify: 102, dtype: object
And if I attempt to drill down and retrieve the mathematics rating of Bob. Our index would even be 1, on condition that math is on the second row
df.iloc[1, 1]
The output can be 58.
Alright, I can wrap this text up right here, however loc and iloc supply some extra spectacular options. Let’s speed-run by means of a few of them.
Extract A number of Rows (Particular College students)
Pandas permits you to retrieve a number of rows utilizing loc and iloc. I’m gonna make an illustration by retrieving the information of a number of college students. On this case, as an alternative of storing a single worth in our loc/iloc technique, we’d be storing an inventory. Right here’s how you are able to do that with loc:
# Alice, Charlie and Edward's information
df.loc[[101, 103, 105]]
Right here’s the output:

And right here’s how to do this with iloc:
df.iloc[[0, 2, 4]]
We’d get the identical output:

I hope you’re getting the grasp of it.
Slice a Vary of Rows
One other useful characteristic Python Pandas presents is the power to slice a spread of rows. Right here, you’ll be able to specify your begin and finish place. Right here’s the syntax for loc/iloc slicing:
df.loc[start_label:end_label]
In loc, nevertheless, the tip label can be included within the output — fairly completely different from the default Python slicing.
The syntax is similar for iloc, with the exception that the tip label can be excluded from the output (similar to the default Python slicing).
Let’s stroll by means of an instance:
I’m making an attempt to retrieve a spread of scholars’ information. Let’s attempt that utilizing loc:
df.loc[101:103]
Output:

As you’ll be able to see above, the tip label is included within the end result. Now, let’s attempt that utilizing iloc. If you happen to recall, the primary row index can be 0, which might imply the third row can be 2.
df.iloc[0:3]
Output:

Right here, the third row is excluded. However for those who’re like me (somebody who questions issues so much), you could be questioning, why would you need the final row to be excluded? In what situations would that be useful? What if I advised you it really makes your life simpler? Let’s clear that up actual fast.
Assuming you need to course of your DataFrame in chunks of 100 rows every.
If slicing had been inclusive, you’d should do some awkward math to keep away from repeating the final row.
However as a result of slicing is unique on the finish, you are able to do this fairly simply, like so.
df.iloc[0:100] # first 100 rows
df.iloc[100:200] # subsequent 100 rows
df.iloc[200:300] # subsequent 100 rows
Right here, there might be no overlaps, and there might be constant chunk sizes. One more reason is the way it’s much like how ranges work in Pandas. Normally, whenever you need to retrieve a spread of rows, it additionally begins at 0 and doesn’t embrace the final row. Having this identical logic in iloc slicing is de facto useful, particularly whenever you’re engaged on some internet scraping or looping by means of a spread of rows.
Extract Particular Columns (Topics)
I’d additionally like to introduce you to the colon : signal. This lets you retrieve all information in your DataFrame utilizing loc. Just like the * in SQL. The cool factor about that is you could filter and extract a subset of columns.
That is normally the place I discover myself beginning. I take advantage of it to get an outline of a selected dataset. From there, I can begin to filter and drill down. Let me present you what I imply.
Let’s retrieve all information:
df.loc[:]
Output:

From right here, I can extract particular columns like so. With loc:
df.loc[:, [‘math’, ‘science’]]
Output:

With iloc:
df.iloc[:, [2, 4]]
The output can be the identical.
I like this characteristic as a result of it’s so versatile. Let’s say I need to retrieve Alice and Bob’s math and science scores. It’ll go one thing like this. I can simply specify the vary of information and columns I need.
With loc:
df.loc[101:103, ['name', 'math', 'science']]
Output:

With iloc:
df.iloc[0:3, [0, 1, 3]]
We’d get the identical output.
Boolean Filtering (Who scored above 80 in Math?)
The ultimate characteristic I need to share with you is Boolean filtering. This permits for a extra versatile extraction. Let’s say I need to retrieve the information of scholars who scored above 80 in Math. Normally, in SQL, you’ll have to make use of the WHERE and HAVING clauses. Python makes this really easy.
# College students with Math > 80.
df.loc[df['math'] > 80]
Output:

You too can filter on a number of circumstances utilizing the AND(&), OR(|), and NOT(~) operators. As an illustration:
# Math > 70 and Science > 80
df.loc[(df[‘math’] > 70) & (df[‘science’] > 80)]
Output:
P.S. I wrote an article on filtering with Pandas. You possibly can learn it right here
Normally, you’ll end up utilizing this characteristic with loc. It may possibly get a bit difficult with iloc, because it doesn’t help Boolean circumstances. To do that with iloc, you’ll should convert the Boolean filtering into an inventory, like so:
# College students with Math > 80.
df.iloc[list(df['math'] > 80)]
To keep away from the headache, simply go along with loc.
Conclusion
You’ll most likely use the loc and iloc strategies so much whenever you’re engaged on a dataset. So it’s essential to know the way they work and distinguish the 2. I like how straightforward and versatile it’s to extract information with these strategies. Everytime you’re confused, simply keep in mind loc is all about labels whereas iloc is about positions.
I hope you discovered this text useful. Strive working these examples by yourself dataset to see the distinction in motion.
I write these articles as a technique to take a look at and strengthen my very own understanding of technical ideas — and to share what I’m studying with others who could be on the identical path. Be happy to share with others. Let’s be taught and develop collectively. Cheers!
Be happy to say hello on any of those platforms
















