Pandas loc vs iloc in Python Data Frame

In this tutorial, we are gonna learn about loc vs iloc in the pandas library of Python. As we know these two functions are used for selecting data from a Pandas Data Frame.

Data extraction is a very essential part of Data analysis, pandas offer 2 ways to extract the rows: –

  1. loc
  2. iloc

iloc function is generally used to locate a row based on numerical indices even if the label to that is different. Whereas, loc function selects data based on the label of the data in the data frame. This will become more clearer after seeing the code.

loc vs iloc in Python

First, we import pandas and make a corresponding data frame to select data from.

CODE: –

import pandas as pd #You can name it anything
dat=pd.DataFrame({'Fruit':['Apple','Peach','Plum'],
                 'Veggies':['Tomato','Capsicum','Cabbage'],
                 'Pulses':['Lentil','Pea','ChickPea']},index=['First','Second','Third'])
dat.head()

In this, I created a data frame of fruit, veggies and pulses using the pd.DataFrame function with the indices as First, Second, Third.

Code Output: –

Pandas loc vs iloc in Python Data Frame

Now we are gonna extract the data for the third row by using numerical index of 2 which represents the Third index: –

CODE: –

dat.iloc[2]
dat.loc[2] #This will throw error as it takes the label('Third') not the numerical one

Code Output: –

Fruit          Plum
Veggies     Cabbage
Pulses     ChickPea
Name: Third, dtype: object

Now we will extract the second row by the use of loc vs iloc and by giving the label now not the index: –

CODE: –

dat.loc['Second']
dat.iloc[1] #This will produce the same result as above command

Code Output: –

Fruit         Peach
Veggies    Capsicum
Pulses          Pea
Name: Second, dtype: object

So, this concludes our tutorial here are some bulletin points: –

  1. Iloc is number based and loc is name based.
  2. Iloc can tell about both the columns and rows whereas loc only tells about rows.
  3. Loc is good for both boolean and non-boolean series whereas iloc does not work for boolean series.

So, when you know the name of row you want to extract go for loc and if you know position go for iloc.

Also read: Multiply two pandas DataFrame columns in Python

Leave a Reply

Your email address will not be published. Required fields are marked *