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:

 NameAgeHeight
1Rohit16150.5
3Alice19145.7
df.iloc[ [0, 1]]#with list objects

Output:

 NameAgeHeight
1Rohit16150.5
2Rahul17167.9
df.iloc[:3]#With slice objects.

Output:

 NameAgeHeight
1Rohit16150.5
2Rahul17167.9
3

Alice

19

145.7

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

Output:

 AgeHeight
116150.5
319145.7