Open In App

Difference between loc() and iloc() in Pandas DataFrame

Last Updated : 04 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Pandas library of Python is very useful for the manipulation of mathematical data and is widely used in the field of machine learning. It comprises many methods for its proper functioning. loc() and iloc() are one of those methods. These are used in slicing data from the Pandas DataFrame. They help in the convenient selection of data from the DataFrame in Python. They are used in filtering the data according to some conditions. 

Difference between loc() and iloc() in Pandas DataFrame

Here, we will see the difference between loc() and iloc() Function in Pandas DataFrame. To see and compare the difference between these two, we will create a sample Dataframe that we will use in the whole paragraph. The working of both of these methods is explained in the sample dataset of cars.

python3




# importing the module
import pandas as pd
 
# creating a sample dataframe
data = pd.DataFrame({'Brand': ['Maruti', 'Hyundai', 'Tata',
                               'Mahindra', 'Maruti', 'Hyundai',
                               'Renault', 'Tata', 'Maruti'],
                     'Year': [2012, 2014, 2011, 2015, 2012,
                              2016, 2014, 2018, 2019],
                     'Kms Driven': [50000, 30000, 60000,
                                    25000, 10000, 46000,
                                    31000, 15000, 12000],
                     'City': ['Gurgaon', 'Delhi', 'Mumbai',
                              'Delhi', 'Mumbai', 'Delhi',
                              'Mumbai', 'Chennai''Ghaziabad'],
                     'Mileage':  [28, 27, 25, 26, 28,
                                  29, 24, 21, 24]})
 
# displaying the DataFrame
display(data)


Output

     Brand  Year  Kms Driven       City  Mileage
0 Maruti 2012 50000 Gurgaon 28
1 Hyundai 2014 30000 Delhi 27
2 Tata 2011 60000 Mumbai 25
3 Mahindra 2015 25000 Delhi 26
4 Maruti 2012 10000 Mumbai 28
5 Hyundai 2016 46000 Delhi 29
6 Renault 2014 31000 Mumbai 24
7 Tata 2018 15000 Chennai 21
8 Maruti 2019 12000 Ghaziabad 24

Python loc() function

The loc() function is label based data selecting method which means that we have to pass the name of the row or column which we want to select. This method includes the last element of the range passed in it, unlike iloc(). loc() can accept the boolean data unlike iloc(). Many operations can be performed using the loc() method like

Example 1: Selecting Data According to Some Conditions

In this example, the code uses the loc function to select and display rows from the DataFrame where the brand is ‘Maruti’ and the mileage is greater than 25, showing relevant information about Maruti cars with high mileage.

python3




# selecting cars with brand 'Maruti' and Mileage > 25
display(data.loc[(data.Brand == 'Maruti') & (data.Mileage > 25)])


Output

    Brand  Year  Kms Driven       City  Mileage
0 Maruti 2012 50000 Gurgaon 28
4 Maruti 2012 10000 Mumbai 28

Example 2: Selecting a Range of Rows From the DataFrame

In this example, the code utilizes the loc function to extract and display rows with indices ranging from 2 to 5 (inclusive) from the DataFrame, providing information about a specific range of cars in the dataset.

python3




# selecting range of rows from 2 to 5
display(data.loc[2: 5])


Output

     Brand  Year  Kms Driven    City  Mileage
2 Tata 2011 60000 Mumbai 25
3 Mahindra 2015 25000 Delhi 26
4 Maruti 2012 10000 Mumbai 28
5 Hyundai 2016 46000 Delhi 29

Example 3: Updating the Value of Any Column

In this example, the code uses the loc function to update the ‘Mileage’ values to 22 for cars in the DataFrame where the manufacturing year is before 2015. The modified DataFrame is then displayed, reflecting the changes made to the Mileage column.

python3




# updating values of Mileage if Year < 2015
data.loc[(data.Year < 2015), ['Mileage']] = 22
display(data)


Output

     Brand  Year  Kms Driven       City  Mileage
0 Maruti 2012 50000 Gurgaon 22
1 Hyundai 2014 30000 Delhi 22
2 Tata 2011 60000 Mumbai 22
3 Mahindra 2015 25000 Delhi 26
4 Maruti 2012 10000 Mumbai 22
5 Hyundai 2016 46000 Delhi 29
6 Renault 2014 31000 Mumbai 22
7 Tata 2018 15000 Chennai 22
8 Maruti 2019 12000 Ghaziabad 22

Python iloc() function

The iloc() function is an indexed-based selecting method which means that we have to pass an integer index in the method to select a specific row/column. This method does not include the last element of the range passed in it unlike loc(). iloc() does not accept the boolean data unlike loc(). Operations performed using iloc() are:

Example 1: Selecting Rows Using Integer Indices

In this example, the code employs the iloc function to extract and display specific rows with indices 0, 2, 4, and 7 from the DataFrame, showcasing information about selected cars in the dataset.

python3




# selecting 0th, 2nd, 4th, and 7th index rows
display(data.iloc[[0, 2, 4, 7]])


Output

     Brand  Year  Kms Driven       City  Mileage
0 Maruti 2012 50000 Gurgaon 28
2 Tata 2011 60000 Mumbai 25
4 Maruti 2012 10000 Mumbai 28
7 Tata 2018 15000 Chennai 21

Example 2: Selecting a Range of Columns and Rows Simultaneously

In this example, the code utilizes the iloc function to extract and display a subset of the DataFrame, including rows 1 to 4 and columns 2 to 4. This provides information about a specific range of cars and their relevant attributes in the dataset.

python3




# selecting rows from 1 to 4 and columns from 2 to 4
display(data.iloc[1: 5, 2: 5])


Output

   Kms Driven    City  Mileage
1 30000 Delhi 27
2 60000 Mumbai 25
3 25000 Delhi 26
4 10000 Mumbai 28



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads