Extracting rows using Pandas .iloc[] in Python

Pandas is a powerful Python library extensively used for data processing and analysis. The .iloc[] method allows you to select rows and columns from a DataFrame using integer-based indexing, making it perfect for positional data extraction.

The iloc method uses zero-based indexing where the first row has index 0, second row has index 1, and so on. Similarly, columns are indexed starting from 0.

Syntax

DataFrame.iloc[row_indexer, column_indexer]

Creating Sample Data

Let's create a sample DataFrame to demonstrate iloc functionality ?

import pandas as pd

# Create sample DataFrame
data = {
    'Id': ['Iris-setosa-1', 'Iris-setosa-2', 'Iris-setosa-3', 'Iris-setosa-4', 'Iris-setosa-5'],
    'SepalLengthCm': [5.1, 4.9, 4.7, 4.6, 5.0],
    'SepalWidthCm': [3.5, 3.0, 3.2, 3.1, 3.6],
    'PetalLengthCm': [1.4, 1.4, 1.3, 1.5, 1.4],
    'PetalWidthCm': [0.2, 0.2, 0.2, 0.2, 0.2]
}

df = pd.DataFrame(data)
print(df)
            Id  SepalLengthCm  SepalWidthCm  PetalLengthCm  PetalWidthCm
0  Iris-setosa-1            5.1           3.5            1.4           0.2
1  Iris-setosa-2            4.9           3.0            1.4           0.2
2  Iris-setosa-3            4.7           3.2            1.3           0.2
3  Iris-setosa-4            4.6           3.1            1.5           0.2
4  Iris-setosa-5            5.0           3.6            1.4           0.2

Selecting Single Row

Extract individual rows by specifying the row index ?

import pandas as pd

# Create sample DataFrame
data = {
    'Id': ['Iris-setosa-1', 'Iris-setosa-2', 'Iris-setosa-3'],
    'SepalLengthCm': [5.1, 4.9, 4.7],
    'SepalWidthCm': [3.5, 3.0, 3.2],
    'PetalLengthCm': [1.4, 1.4, 1.3]
}

df = pd.DataFrame(data)

# Select first row (index 0)
row0 = df.iloc[0]
print("First row:")
print(row0)
print()

# Select second row (index 1)
row1 = df.iloc[1]
print("Second row:")
print(row1)
First row:
Id               Iris-setosa-1
SepalLengthCm              5.1
SepalWidthCm               3.5
PetalLengthCm              1.4
Name: 0, dtype: object

Second row:
Id               Iris-setosa-2
SepalLengthCm              4.9
SepalWidthCm                 3
PetalLengthCm              1.4
Name: 1, dtype: object

Selecting Multiple Rows

Use slicing to select multiple consecutive rows ?

import pandas as pd

# Create sample DataFrame
data = {
    'Id': ['Iris-setosa-1', 'Iris-setosa-2', 'Iris-setosa-3', 'Iris-setosa-4', 'Iris-setosa-5'],
    'SepalLengthCm': [5.1, 4.9, 4.7, 4.6, 5.0],
    'SepalWidthCm': [3.5, 3.0, 3.2, 3.1, 3.6],
    'PetalLengthCm': [1.4, 1.4, 1.3, 1.5, 1.4]
}

df = pd.DataFrame(data)

# Select rows 1 to 3 (index 1, 2, 3)
rows = df.iloc[1:4]
print(rows)
            Id  SepalLengthCm  SepalWidthCm  PetalLengthCm
1  Iris-setosa-2            4.9           3.0            1.4
2  Iris-setosa-3            4.7           3.2            1.3
3  Iris-setosa-4            4.6           3.1            1.5

Selecting Rows and Columns

Specify both row and column ranges to extract specific data subsets ?

import pandas as pd

# Create sample DataFrame
data = {
    'Id': ['Iris-setosa-1', 'Iris-setosa-2', 'Iris-setosa-3', 'Iris-setosa-4', 'Iris-setosa-5'],
    'SepalLengthCm': [5.1, 4.9, 4.7, 4.6, 5.0],
    'SepalWidthCm': [3.5, 3.0, 3.2, 3.1, 3.6],
    'PetalLengthCm': [1.4, 1.4, 1.3, 1.5, 1.4]
}

df = pd.DataFrame(data)

# Select rows 1-3 and first 2 columns
rows_columns = df.iloc[1:4, 0:2]
print(rows_columns)
            Id  SepalLengthCm
1  Iris-setosa-2            4.9
2  Iris-setosa-3            4.7
3  Iris-setosa-4            4.6

Using List of Indices

Select non-consecutive rows and columns using lists ?

import pandas as pd

# Create sample DataFrame
data = {
    'Id': ['Iris-setosa-1', 'Iris-setosa-2', 'Iris-setosa-3', 'Iris-setosa-4', 'Iris-setosa-5'],
    'SepalLengthCm': [5.1, 4.9, 4.7, 4.6, 5.0],
    'SepalWidthCm': [3.5, 3.0, 3.2, 3.1, 3.6],
    'PetalLengthCm': [1.4, 1.4, 1.3, 1.5, 1.4]
}

df = pd.DataFrame(data)

# Select specific rows and columns
result = df.iloc[[0, 2, 4], [0, 2]]
print(result)
            Id  SepalWidthCm
0  Iris-setosa-1           3.5
2  Iris-setosa-3           3.2
4  Iris-setosa-5           3.6

Common Use Cases

Operation Syntax Description
Single row df.iloc[0] First row as Series
Multiple rows df.iloc[1:4] Rows 1, 2, 3
Rows + Columns df.iloc[1:4, 0:2] Rows 1-3, Columns 0-1
Specific indices df.iloc[[0,2], [1,3]] Rows 0,2 and Columns 1,3

Conclusion

The .iloc[] method provides powerful integer-based indexing for DataFrame row and column selection. It's essential for positional data extraction and works great with slicing and list-based indexing for flexible data filtering.

Updated on: 2026-03-15T18:23:33+05:30

765 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements