Difference between loc() and iloc() in Pandas DataFrame
Last Updated :
07 May, 2024
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
import pandas as pd
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 ]})
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
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.
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
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 21
8 Maruti 2019 12000 Ghaziabad 24
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
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
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
Similar Reads
Pandas - Find the Difference between two Dataframes
In this article, we will discuss how to compare two DataFrames in pandas. First, let's create two DataFrames. Creating two dataframes [GFGTABS] Python3 import pandas as pd # first dataframe df1 = pd.DataFrame({ 'Age': ['20', '14', '56', '28', '10'],
2 min read
Create a Pandas DataFrame from List of Dicts
Pandas DataFrame is a 2-dimensional labeled data structure with columns of potentially different types. It is generally the most commonly used Pandas object. Pandas DataFrame can be created in multiple ways using Python. Letâs discuss how to create a Pandas DataFrame from the List of Dictionaries. C
3 min read
Difference Between Shallow copy VS Deep copy in Pandas Dataframes
The pandas library has mainly two data structures DataFrames and Series. These data structures are internally represented with index arrays, which label the data, and data arrays, which contain the actual data. Now, when we try to copy these data structures (DataFrames and Series) we essentially cop
4 min read
What is the difference between join and merge in Pandas?
In Pandas, join() combines DataFrames based on their indices and defaults to a left join, while merge() joins on specified columns and defaults to an inner join. Choosing the right method depends on how your data is aligned. To illustrate the difference between join() and merge() visually, Let's und
4 min read
Create a list from rows in Pandas dataframe
Python lists are one of the most versatile data structures, offering a range of built-in functions for efficient data manipulation. When working with Pandas, we often need to extract entire rows from a DataFrame and store them in a list for further processing. Unlike columns, which are easily access
5 min read
Python Pandas - Difference between INNER JOIN and LEFT SEMI JOIN
In this article, we see the difference between INNER JOIN and LEFT SEMI JOIN. Inner Join An inner join requires two data set columns to be the same to fetch the common row data values or data from the data table. In simple words, and returns a data frame or values with only those rows in the data fr
3 min read
Difference between Pandas VS NumPy
Python is one of the most popular languages for Machine Learning, Data Analysis, and Deep learning tasks. It is powerful because of its libraries that provide the user full command over the data. Today, we will look into the most popular libraries i.e. NumPy and Pandas in Python, and then we will co
3 min read
Create a Pandas DataFrame from Lists
Converting lists to DataFrames is crucial in data analysis, Pandas enabling you to perform sophisticated data manipulations and analyses with ease. List to Dataframe Example # Simple listdata = [1, 2, 3, 4, 5]# Convert to DataFramedf = pd.DataFrame(data, columns=['Numbers'])Here we will discuss diff
5 min read
Conversion Functions in Pandas DataFrame
.numpy-table { font-family: arial, sans-serif; border-collapse: collapse; border: 1px solid #5fb962; width: 100%; } .numpy-table td, th { background-color: #c6ebd9; border: 1px solid #5fb962; text-align: left; padding: 8px; } .numpy-table td:nth-child(odd) { background-color: #c6ebd9; } .numpy-table
5 min read
Different ways to create Pandas Dataframe
It is the most commonly used Pandas object. The pd.DataFrame() function is used to create a DataFrame in Pandas. There are several ways to create a Pandas Dataframe in Python. Example: Creating a DataFrame from a Dictionary [GFGTABS] Python import pandas as pd # initialize data of lists. data = {
7 min read