Skip to main content

Python Pandas DataFrame Property: iloc

In Pandas DataFrame .iloc() is a integer based indexing for selection by position. It only accepts integer type values as index values for the values to be accessed and displayed. iloc() funtion is used to retrieve a particular value belonging to a row abd column using the index value assigned to it.

Allowed inputs are:

  • Integer e.g. 6.
  • List or array of integer e.g. [4,7,8].
  • Slice Objects with intger
  • Boolean Array
  • Callable Function with one argument.

This method will raise a IndexError, if a requested indexer is out-of-bounds, except slice indexers which allow out-of-bounds indexing.

Syntax

dataframe.iloc[row, column]

Example

import pandas as pd
df=pd.DataFrame({'Name':['Rohit','Rahul','Alice','John','Joey'],'Age':[16,17,19,15,14],
                 'Height':[150.5,167.9,145.7,152.6,148.7]})

index = ['1', '2', '3', '4', '5']
df.index=index 
print(df)

Output:

    Name  Age  Height
1  Rohit   16   150.5
2  Rahul   17   167.9
3  Alice   19   145.7
4   John   15   152.6
5   Joey   14   148.7
df.iloc[[0, 2]]

Output:

  Name Age Height
1 Rohit 16 150.5
3 Alice 19 145.7
df.iloc[ [0, 1]]#with list objects

Output:

  Name Age Height
1 Rohit 16 150.5
2 Rahul 17 167.9
df.iloc[:3]#With slice objects.

Output:

  Name Age Height
1 Rohit 16 150.5
2 Rahul 17 167.9
3

Alice

19

145.7

df.iloc[[0, 2], [1, 2]]#Indexing both axes With lists of integers.

Output:

  Age Height
1 16 150.5
3 19 145.7
Submitted by devanshi.srivastava on November 15, 2021

Devanshi, is working as a Data Scientist with iVagus. She has expertise in Python, NumPy, Pandas and other data science technologies.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.