The iloc[]
property in Pandas allows us to select rows and columns based on their integer location.
Example
import pandas as pd
data = {'A': [1, 2, 3],
'B': [4, 5, 6]}
df = pd.DataFrame(data)
# select the element in the first row and second column
element = df.iloc[0, 1]
print(element)
# Output: 4
iloc[] Syntax
The syntax of the iloc[]
property in Pandas is:
iloc[row_selection, column_selection]
iloc[] Arguments
The iloc[]
property takes following arguments:
row_selection
- specifies which rows you want to select based on integer positionscolumn_selection
- specifies which columns you want to select based on integer positions
iloc[] Return Value
The iloc[]
property in Pandas returns a subset of a DataFrame or Series based on the integer-location-based indexing you specify.
Example 1: Select Single Element From a DataFrame
import pandas as pd
data = {'Student_ID': [101, 102, 103],
'Name': ['Alice', 'Bob', 'Charlie'],
'Score': [85, 92, 78]}
df = pd.DataFrame(data)
# select the score of the first student (first row, third column)
first_student_score = df.iloc[0, 2]
print("Score of the first student:", first_student_score)
Output
Score of the first student: 85
In the above example, we have the df DataFrame representing student data with columns: Student_ID
, Name
, and Score
.
We used iloc[0,2]
to select the score of the first student, which is located in the first row (index 0) and the third column (index 2).
Example 2: Select Multiple Rows and Columns Using Slices
import pandas as pd
data = {'Student_ID': [101, 102, 103, 104, 105],
'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
'Math_Score': [85, 92, 78, 90, 88],
'Science_Score': [88, 94, 76, 89, 84]}
df = pd.DataFrame(data)
# select rows 1 to 3 (exclusive) and columns 1 to 3 (exclusive)
selected_data = df.iloc[1:3, 1:3]
print(selected_data)
Output
Name Math_Score 1 Bob 92 2 Charlie 78
Here, we used iloc[1:3, 1:3]
to select a slice of rows from index 1 (inclusive) to index 3 (exclusive) and a slice of columns from index 1 (inclusive) to index 3 (exclusive).
As a result, data from rows 1 and 2 of columns 'Name'
and 'Math_Score'
are displayed.
Note: To learn more about how slicing works, please visit Pandas Indexing and Slicing.
Example 3: Select Specific Rows and Columns Using Lists of Integers
import pandas as pd
data = {'Student_ID': [101, 102, 103],
'Name': ['Alice', 'Bob', 'Charlie'],
'Math_Score': [85, 92, 78],
'Science_Score': [90, 88, 82]}
df = pd.DataFrame(data)
# select specific rows (rows 0 and 2) and specific columns (columns 1 and 3)
selected_data = df.iloc[[0, 2], [1, 3]]
print(selected_data)
Output
Name Science_Score 0 Alice 90 2 Charlie 82
In the above example, we used iloc[[0, 2], [1, 3]]
to select rows with indices 0 and 2 (which are the first and third rows) and columns with indices 1 and 3 (which are the second and fourth columns).
The result is a new DataFrame containing the selected rows and columns.
Example 4: Select all Rows for Specific Columns
import pandas as pd
data = {'Student_ID': [101, 102, 103],
'Name': ['Alice', 'Bob', 'Charlie'],
'Score': [85, 92, 78]}
df = pd.DataFrame(data)
# select all rows for the 'Student_ID' and 'Name' columns
selected_data = df.iloc[:, [0, 1]]
print(selected_data)
Output
Student_ID Name 0 101 Alice 1 102 Bob 2 103 Charlie
Here, the iloc[:, [0, 1]]
property selects all rows and the columns at index 0, 'Student_ID'
, and index 1, 'Name'
.
This will give us a DataFrame containing all rows for the 'Student_ID'
and 'Name'
columns.
Example 5: Select Specific Rows For All Columns
import pandas as pd
data = {'Student_ID': [101, 102, 103, 104, 105],
'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eve'],
'Score': [85, 92, 78, 88, 95]}
df = pd.DataFrame(data)
# select rows 1 and 3 for all columns
selected_rows = df.iloc[[1, 3], :]
print(selected_rows)
Output
Student_ID Name Score
1 102 Bob 92
3 104 David 88
Here, iloc[[1, 3], :]
allows us to select the first and third row while including all columns.