Home »
Python »
Python Programs
Difference between loc() and iloc() in Pandas DataFrame
Learn about the basic difference between the loc() and iloc() methods of Python Pandas DataFrame.
Submitted by Pranit Sharma, on April 18, 2022
Both loc() and iloc() methods are used for slicing the data from the pandas DataFrame. Both of these methods are effective and efficient ways of selecting rows or columns. Also, these methods require some kind of condition as a parameter based on which it filters out the data from the DataFrame.
Let us understand the example given below:
# Importing pandas package
import pandas as pd
# Creating a dictionary of student marks
d = {
"Peter":[65,70,70,75],
"Harry":[45,56,66,66],
"Tom":[67,87,65,53],
"John":[56,78,65,64]
}
# Now, Create DataFrame and assign index
# name as subject names
df = pd.DataFrame(d,index=["Maths","Physics","Chemistry","English"])
# Printing the DataFrame
print(df,"\n")
Output:
Peter Harry Tom John
Maths 65 45 67 56
Physics 70 56 87 78
Chemistry 70 66 65 65
English 75 66 53 64
In the above example, we have created a DataFrame. Now we will use the loc() and iloc() methods and observer the difference between them.
Using loc() Method
The loc() method is a type of data selection method which takes the name of a row or column as a parameter. To perform various operations using the loc() method, we need to pass the required condition of rows and columns to get the filtered data.
# Selecting a row value where column name
# is Harry and its value is 66
print(df.loc[df['Harry']==66],"\n")
# Selecting a row where column name
# is Peter and its value is 70
print(df.loc[df['Peter']==70])
Output:
Peter Harry Tom John
Maths 65 45 67 56
Physics 70 56 87 78
Chemistry 70 66 65 65
English 75 66 53 64
Peter Harry Tom John
Chemistry 70 66 65 65
English 75 66 53 64
Peter Harry Tom John
Physics 70 56 87 78
Chemistry 70 66 65 65
Here, the loc() method returns the entire row where marks of 'Harry' and 'Peter' are 66 and 70 respectively. In this way, loc() method works upon a particular condition.
Using iloc() Method
The 'i' in iloc() stands for index. This is also a data selection method but here, we need to pass the proper index as a parameter to select the required row or column. Index are nothing but the integer value ranging from 0 to n-1 which represents the number of rows or columns. We can perform various operations using iloc() method. Inside iloc() method, the index value of the row comes first followed by the number of columns.
# Selecting 0th & 2nd index rows
print(df.iloc[[0, 2,]])
# Selecting specified rows and specified columns
print(df.iloc[0:3,1:2])
Output:
Peter Harry Tom John
Maths 65 45 67 56
Physics 70 56 87 78
Chemistry 70 66 65 65
English 75 66 53 64
Peter Harry Tom John
Maths 65 45 67 56
Chemistry 70 66 65 65
Harry
Maths 45
Physics 56
Chemistry 66
Here, first, we have passed a list of indices which means all those rows which we want to be displayed, and second time, we have passed the sliced index without any list, which means we want the rows from 0 to 2 followed by the columns number 1.
Note: The slicing method does not consider the end value i.e., the value specified after the colon (:).
Python Pandas Programs »