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

20 Aug, 2020
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 of many methods for its proper functioning. loc() and iloc() are one of those methods. These are used in slicing of data from the Pandas DataFrame. They help in the convenient selection of data from the DataFrame. They are used in filtering the data according to some conditions. Working of both of these methods is explained in the sample dataset of cars.
# importing the moduleimport pandas as pd   # creating a sample dataframedata = 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 DataFramedisplay(data)
Output :
loc() : loc() 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-
1. Selecting data according to some conditions :
# selecting cars with brand 'Maruti' and Mileage > 25display(data.loc[(data.Brand == 'Maruti') & (data.Mileage > 25)])
Output :
2. Selecting a range of rows from the DataFrame :
# selecting range of rows from 2 to 5display(data.loc[2 : 5])
Output :
3. Updating the value of any column :
# updating values of Mileage if Year < 2015data.loc[(data.Year < 2015), ['Mileage']] = 22display(data)
Output :
iloc() : iloc() is a indexed based selecting method which means that we have to pass integer index in the method to select 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:
1. Selecting rows using integer indices:
# selecting 0th, 2th, 4th, and 7th index rowsdisplay(data.iloc[[0, 2, 4, 7]])
Output :
2. Selecting a range of columns and rows simultaneously:
# selecting rows from 1 to 4 and columns from 2 to 4display(data.iloc[1 : 5, 2 : 5])
Output :
Python pandas-dataFrame
Python pandas-dataFrame-methods
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python program to convert a list to string
Python String | replace()
Reading and Writing to text files in Python
sum() function in Python