Difference Between loc and iloc Properties in Pandas DataFrame

In this tutorial, we will learn about the difference between loc[] and iloc[] properties in Pandas DataFrame with the help of examples. By Pranit Sharma Last updated : April 12, 2023

pandas.DataFrame.loc Property

The loc[] property 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[] property, we need to pass the required condition of rows and columns to get the filtered data.

Syntax

property DataFrame.loc

Example

# 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")

# 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

pandas.DataFrame.iloc Property

The 'i' in iloc[] property 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[] property. Inside iloc[], the index value of the row comes first followed by the number of columns.

Syntax

property DataFrame.iloc

Example

# 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")

# 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

Difference Between loc and iloc Properties in Pandas DataFrame

The main difference between .loc and .iloc in Panda DataFrame is that .loc accesses a group of rows and columns by label(s) or a Boolean array. Whereas, .iloc is purely integer-location based indexing for selection by position.

Conclusion (.loc Vs .iloc)

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 »

All Python Pandas Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.