Python iloc() Function | Explained With Examples

Pandas, a popular data manipulation module, provides many functions to manipulate or organize data. The operations like reading data, removing data, appending data, converting data format, etc., are performed using the multiple functions of the Pandas module. The “iloc()” function of the Pandas module allows the users to select single or multiple data values from the given dataset. 

In this post, we will examine the working of Python’s “iloc()” function using numerous examples.

What is the Python iloc() Function?

In Python, the “iloc()” function of the Pandas library is used for indexing and selecting specific data from a DataFrame or dataset according to the index value. The syntax of the Python “iloc()” function is shown below:

dataframe.iloc[row, column]

Here, the parameter “row” is used to take the index value and return a single row of data from the given dataset. Similarly, the parameter “column” specifies the column’s index value.

This is the dataset that is being used in upcoming examples of the Python “iloc()” function:

Example 1: Access Data of the Specific Index of the Dataset 

To access data of the specific index of a dataset, the “iloc()” function is used:

Code:

import pandas
data_value = pandas.read_csv("example.csv")
print(data_value.iloc[5])
  • The “pandas” module is imported.
  • The “pd.read_csv()” function takes the dataset from the csv file named “example.csv” as an argument and reads the data.
  • The “iloc()” function accepts the integer value of the index and reads all the data of columns located at index 3.

Output:

The data values at index “3” have been read successfully.

Example 2: Access Data of the Dataset Within Specific Range

The following code is used to access the range of data from the dataset using the index values:

Code:

import pandas
data_value = pandas.read_csv("example.csv")
print(data_value.iloc[3:5])
  • The “pandas” module is imported.
  • The “pd.read_csv()” reads the data from the file named “example.csv”.
  • The “iloc()” function takes the index range “[3:5]” as an argument and returns the specific data.

Output:

The data of the index position “3” and “4” has been read successfully.

Example 3: Access Columns Value of the Dataset

The Python “iloc()” function can access the column value of the given dataset using the column index value. Here is an example code:

Code:

import pandas
data_value = pandas.read_csv("example.csv")
print(data_value.iloc[:,1:3])

The “[:,1:3]” expression is passed as a parameter to the “iloc()” function to access the data values of columns 1 and 2.

Output:

The data values of columns 1 and 2 have been read successfully.

Conclusion

The Python “iloc()” function is used to get the specific single or multiple data values from the dataset using the index position. The range of the index is passed as an argument to Python’s “iloc()” function to get the multiple data values. The column data value can also be accessed using the “iloc()” function. This guide provided a thorough guide on Python’s “iloc()” function using appropriate examples.