Pandas Slice Rows

Last Updated : 23 Jul, 2025

Simplest way to select rows is by using the iloc method which allows to access rows by their integer position. For instance, to slice a single row you can specify its index.

Python
import pandas as pd

data = {'Name': ['John', 'Alice', 'Bob', 'Eve'], 
        'Age': [25, 30, 22, 35], 
        'Gender': ['Male', 'Female', 'Male', 'Female'], 
        'Salary': [50000, 55000, 40000, 70000]}

df = pd.DataFrame(data)
# Slice rows from position 1 to 3 (exclusive of 3)
sliced_rows = df.iloc[1:3]
print(sliced_rows)

Output
    Name  Age  Gender  Salary
1  Alice   30  Female   55000
2    Bob   22    Male   40000

In addition to the this method, there are other methods to do the same:

Methid 1: Slicing Rows Using loc with Conditions

The loc[] method allows you to slice rows based on conditions or index labels. This is useful when you want to filter rows that meet specific criteria.

Python
# Slice rows where Age is greater than 25
filtered_rows = df.loc[df['Age'] > 25]
print(filtered_rows)

Output
    Name  Age  Gender  Salary
1  Alice   30  Female   55000
3    Eve   35  Female   70000

Method 2: Slicing Rows Using head and tail

The head() and tail() methods provide a quick way to slice the first or last few rows of a DataFrame. This is useful when you want to inspect the top or bottom portion of your data.

Python
# Get the first 3 rows
top_rows = df.head(3)
print(top_rows)

Output
    Name  Age  Gender  Salary
0   John   25    Male   50000
1  Alice   30  Female   55000
2    Bob   22    Male   40000
Python
# Get the last 2 rows
bottom_rows = df.tail(2)
print(bottom_rows)

Output
  Name  Age  Gender  Salary
2  Bob   22    Male   40000
3  Eve   35  Female   70000
Comment

Explore