Updated April 10, 2023
Introduction to Pandas Dataframe.iloc[]
Pandas Dataframe.iloc[] is essentially integer number position which is based on 0 to length-1 of the axis, however, it may likewise be utilized with a Boolean exhibit.
The .iloc[] function is utilized to access all the rows and columns as a Boolean array.
Syntax for Pandas Dataframe .iloc[] is:
Series.iloc
This .iloc[] function allows 5 different types of inputs.
- An integer:Example: 7
- A Boolean Array
- A callable function which is accessing the series or Dataframe and it returns the result to the index.
- A list of arrays of integers: Example: [2,4,6]
- A slice object with ints: Example: 2:5
How Pandas Dataframe.iloc[] Works?
The simple examples below show how Pandas Dataframe .iloc[] function works.
Example #1
To select a single row from the Dataframe
Code:
import pandas as pd
data = { 'country':['Canada', 'Portugal', 'Ireland', 'Nigeria', 'Brazil', 'India']
,'continent':['America','Europe','Europe','Africa','SA','Asia']
}
df = pd.DataFrame(data, columns = ['country', 'continent'])
df.iloc[0]
print(df.iloc[0])
Output:
Here, we first import Pandas and create a dataframe. Once the Dataframe is created, the .iloc function is invoked. So, we select the 0th array in the data and print only the 0th row as our output.
Example #2
This is an alternate method of selecting a single row from the Dataframe using the .iloc() function.
Code:
import pandas as pd
data = { 'country':['Canada', 'Portugal', 'Ireland', 'Nigeria', 'Brazil', 'India']
,'continent':['America','Europe','Europe','Africa','SA','Asia']
}
df = pd.DataFrame(data, columns = ['country', 'continent'])
df.iloc[0]
print(df.iloc[0,:])
Output:
Explanation: This also produces the same output as the previous one but here we add a colon to the .iloc() function because we want to specifically represent the 0th column and we want all the data to be present. In this new syntax, we also observe that the integer value remains the same as the previous code which is enclosed in square brackets. The integer value represents that we want to consider the data in the index This kind of representation is mainly used in selecting columns more than selecting rows.This is significant, really, in light of the fact that the linguistic structure is increasingly predictable with the sentence structure that we are going to use to choose segments, and to recover “slices” of information.
Example #3
This is an alternate method of selecting a single row from the Dataframe using the .iloc() function.
Code:
import pandas as pd
data = { 'country':['Canada', 'Portugal', 'Ireland', 'Nigeria', 'Brazil', 'India']
,'continent':['America','Europe','Europe','Africa','SA','Asia']
}
df = pd.DataFrame(data, columns = ['country', 'continent'])
df.iloc[0]
print(df.iloc[:,0])
Output:
Explanation: Now when we speak about slicing the objects from the Pandas Dataframe, we look at how to select columns as we previously discussed the syntax to select rows. Now if we want to print the data which is there in the first column, we shift the integer value to the second place and add a “:” in the first place. Hence, the integer always signifies the column which we should consider and print. So, the “:” here represents the rows which we want to print. Since, it is in the first position, we get the 1st column which we want and the rows. This shows we need to recover the entirety of the lines. Keep in mind, the primary list position within iloc[] indicates the rows, and when we utilize the ‘:’ character, we are advising Pandas to recover the entirety of the columns.
Example #4
Code:
import pandas as pd
data = { 'country':['Canada', 'Portugal', 'Ireland', 'Nigeria', 'Brazil', 'India']
,'continent':['America','Europe','Europe','Africa','SA','Asia']
}
df = pd.DataFrame(data, columns = ['country', 'continent'])
df.iloc[3,0]
print(df.iloc[3,0])
Output:
Explanation: In the above program, we will pick the data in a specific cell in the DataFrame. You will basically use iloc[] and show an integer index value you want to print for the data in the row and column you have to recoup. So if we have to pick the data in row3 and column 0, we’ll use the above code. Utilizing the primary list position, we indicated that we need the information from row index 3, and we utilized the subsequent file position to determine that we need to recover the data in column index 0. The information that fits the two standards is Nigeria, in cell (3, 0). Hence, Pandas DataFrame basically works like an Excel spreadsheet. You can simply determine the line and segment of the information that you need to print.
Example #5
Slicing is basically considering and implementing multiple rows and multiple columns. To slice multiple rows, we use the following code:
Code:
import pandas as pd
data = { 'country':['Canada', 'Portugal', 'Ireland', 'Nigeria', 'Brazil', 'India']
,'continent':['America','Europe','Europe','Africa','SA','Asia']
}
df = pd.DataFrame(data, columns = ['country', 'continent'])
df.iloc[0:4]
print(df.iloc[0:4])
Output:
Explanation: Here, we will determine our DataFrame, df, and afterward, call the iloc[] technique utilizing spot documentation. At that point, within the iloc technique, we will indicate the beginning line and stop push lists, isolated by a colon.
Example #6
To slice multiple columns, we use the following code:
Code:
import pandas as pd
data = { 'country':['Canada', 'Portugal', 'Ireland', 'Nigeria', 'Brazil', 'India']
,'continent':['America','Europe','Europe','Africa','SA','Asia']
}
df = pd.DataFrame(data, columns = ['country', 'continent'])
df.iloc[:,0:4]
print(df.iloc[:,0:4])
Output:
Explanation: In the above program, we will implement the subset of columns. Here, we will determine that we are going to utilize information from df. At that point we will utilize spot documentation to call the iloc[] strategy following the name of the DataFrame. Within the iloc[] strategy, we are utilizing the “:” character for the line record. This implies we need to recover all lines. For the section record, we are utilizing the range 0:4. This implies we need to recover the sections beginning from segment 0 up to and barring segment 4.
Conclusion – Pandas Dataframe.iloc[]
I conclude by saying that data manipulation is a very critical yet beautiful topic in Data Science. These .iloc() functions mainly focus on data manipulation in Pandas Dataframe. The iloc strategy empowers you to “find” a row or column by its “integer index.”We utilize the integer index values to find rows, columns, and perceptions.The request for the indices inside the brackets clearly matters. The primary record number will be the row or column that you need to recover. At that point, the subsequent record is the row or column that you need to recover. Significantly, the column record is discretionary.
Recommended Articles
We hope that this EDUCBA information on “Pandas Dataframe.iloc[]” was beneficial to you. You can view EDUCBA’s recommended articles for more information.