Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
wb_sunny

Python iloc() function – All you need to know!

Python Iloc() Function

Hello, folks! In this article, we will be focusing on the working of Python iloc() function. So, let us get started!


Working of the Python iloc() function

Python offers us with various modules and functions to deal with the data. Pandas module offers us more of the functions to deal with huge datasets altogether in terms of rows and columns.

Python iloc() function enables us to select a particular cell of the dataset, that is, it helps us select a value that belongs to a particular row or column from a set of values of a data frame or dataset.

With iloc() function, we can retrieve a particular value belonging to a row and column using the index values assigned to it.

Remember, iloc() function accepts only integer type values as the index values for the values to be accessed and displayed.

Syntax:

dataframe.iloc[]

As mentioned, we cannot pass boolean values as an index to retrieve the records. It is mandatory to pass integer values to it.


Examples of the Python iloc() function

In this example, we have tried to access all the data values of the third index of every column of the dataset as shown.

Dataset– The dataset being used in the below examples

Dataset
Dataset

Example 1:

We’ll use the read_csv() function to pull in the data set from CSV into a variable.

import pandas as pd
import numpy as np
import os
data = pd.read_csv("bank-loan.csv") # dataset
print(data.iloc[3])

Output:

age          41.00000
ed            1.00000
employ       15.00000
address      14.00000
income      120.00000
debtinc       2.90000
creddebt      2.65872
othdebt       0.82128
default       0.00000
Name: 3, dtype: float64

Now, we have tried to access the data values of the rows 1 and 2 equivalent to every column of the dataset as shown below–

Example 2:

import pandas as pd
import numpy as np
import os
data = pd.read_csv("bank-loan.csv") # dataset
data.iloc[1:3]

The function iloc[1:3] would include the from 1 upto 3 and does not include the index 3.

Output:

Python iloc() Example 2
Python iloc() Example 2

Here, we have accessed all the data values of the columns 1 and 2 as shown below–

Syntax:

dataframe.iloc[:,start_col:end_col]

Example 3:

import pandas as pd
import numpy as np
import os
data = pd.read_csv("bank-loan.csv") # dataset
data.iloc[:,1:3]

Output:

Python iloc() Example 3
Python iloc() Example 3

Summary

Thus, in this article, we have understood the functioning of Python iloc() function.

  • It can be used to fetch records based on the index values from the datasets.
  • Multiple records can be fetched using the concept of index as a parameter to the iloc() function.
  • The iloc() function considers only integer indexes as parameters.

Conclusion

By this, we have come to the end of this topic. Feel free to comment below in case you have any questions.

Till then, Stay tuned and Keep Learning!!