What is iloc in pandas?

The pandas library in Python is used to work with data framesstructure data in rows and columns. pandas is widely used in Data Analysis and Machine Learning.

The iloc operator is used to index a portion of the data frame. It uses index positions to index rows and columns. We can also use it to index boolean expressions.

Index positions start from 0.

The syntax of iloc is as follows:

dataframe.iloc[rows index, column index]

End points are exclusive in iloc when the range operator (:) is used.

We can mention row numbers in the form of a range, such as 0:5. The syntax will be as follows:

dataframe.iloc[0:5, 0]

Similarly, we can index individual rows randomly. For that, we enclose the row numbers in the form of a list using [ ]. The syntax will be:

dataframe.iloc[[0,1,2,3], 0]

We can index a single column as well. The syntax will be:

dataframe.iloc[[0,1,2,3], 0]

If we do not enclose the column index with a [ ], a series is returned.

When you use [ ] around the column index, it will return a data frame. The syntax is as follows:

dataframe.iloc[[0,1,2,3], [0]]

We can also index several columns together by enclosing column numbers in the form of a list using [ ]. The syntax will be:

dataframe.iloc[[0,1,2,3], [0,2,4,5]]

Example

The code snippet below shows how iloc can be used:

import pandas as pd
# Creating a dataframe
df = pd.DataFrame({'Sports': ['Football', 'Cricket', 'Baseball', 'Basketball',
'Tennis', 'Table-tennis', 'Archery', 'Swimming', 'Boxing'],
'Player': ["Messi", "Afridi", "Chad", "Johnny", "Federer",
"Yong", "Mark", "Phelps", "Khan"],
'Rank': [1, 9, 7, 12, 1, 2, 11, 1, 1] })
print(df.iloc[0:5, [0,1,2] ]) # using row range and multiple columns
print('\n')
print(df.iloc[[1,2,3], 0]) # Using specific rows and returning a series
print('\n')
print(df.iloc[[1,2,3], [2]]) # Using specific rows and returning a dataframe

Indexing using a boolean expression

We can index the data frame by placing boolean expressions within iloc. The syntax is as follows:

dataframe.loc[expression]

Boolean expressions use conditions and operators such as ==, >, and <.

Example

The code snippet below shows iloc using boolean expressions:

import pandas as pd
# Creating a dataframe
df = pd.DataFrame({'Sports': ['Football', 'Cricket', 'Baseball', 'Basketball',
'Tennis', 'Table-tennis', 'Archery', 'Swimming', 'Boxing'],
'Player': ["Messi", "Afridi", "Chad", "Johnny", "Federer",
"Yong", "Mark", "Phelps", "Khan"],
'Rank': [1, 9, 7, 12, 1, 2, 11, 1, 1] })
bool_list = [True,False,True,True,True,False,True,True,True,False]
print(df.iloc[bool_list])

Explanation

In the code snippet above, we have passed a boolean array to the iloc operator. A boolean array has the same result as a boolean expression. Indexes whose value is true will be shown in the result of the iloc operator. Indexes whose value is false will not be shown.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved