![]() |
VOOZH | about |
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.
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.
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
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
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.
Output
Brand Year Kms Driven City Mileage
0 Maruti 2012 50000 Gurgaon 28
4 Maruti 2012 10000 Mumbai 28
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.
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
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.
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 21
8 Maruti 2019 12000 Ghaziabad 24
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:
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.
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
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.
Output
Kms Driven City Mileage
1 30000 Delhi 27
2 60000 Mumbai 25
3 25000 Delhi 26
4 10000 Mumbai 28