Python Pandas - How to create a RangeIndex

A RangeIndex is a memory-efficient special case of Int64Index in Pandas that represents monotonic ranges. It's particularly useful for creating sequential indices without storing every individual value in memory.

What is RangeIndex?

RangeIndex is similar to Python's range() function but designed for Pandas indexing. It stores only the start, stop, and step values, making it memory-efficient for large sequential indices.

Basic Syntax

pd.RangeIndex(start=0, stop=None, step=1, name=None)

Creating a Simple RangeIndex

Here's how to create a basic RangeIndex with default parameters ?

import pandas as pd

# Create a simple RangeIndex from 0 to 4
index = pd.RangeIndex(5)
print("Simple RangeIndex:")
print(index)
Simple RangeIndex:
RangeIndex(start=0, stop=5, step=1)

Creating RangeIndex with Custom Parameters

You can specify start, stop, step, and name parameters ?

import pandas as pd

# Create a range index with start, stop and step
# The name is the name to be stored in the index
index = pd.RangeIndex(start=10, stop=30, step=2, name="data")

# Display the RangeIndex
print("RangeIndex...")
print(index)

# Display the start parameter value
print("\nRangeIndex start value...")
print(index.start)

# Display the stop parameter value
print("\nRangeIndex stop value...")
print(index.stop)

# Display the step parameter value
print("\nRangeIndex step value...")
print(index.step)
RangeIndex...
RangeIndex(start=10, stop=30, step=2, name='data')

RangeIndex start value...
10

RangeIndex stop value...
30

RangeIndex step value...
2

Using RangeIndex with DataFrames

RangeIndex is commonly used as DataFrame indices ?

import pandas as pd

# Create DataFrame with custom RangeIndex
data = {'A': [1, 2, 3, 4, 5], 'B': [10, 20, 30, 40, 50]}
df = pd.DataFrame(data, index=pd.RangeIndex(start=100, stop=110, step=2))

print("DataFrame with RangeIndex:")
print(df)
DataFrame with RangeIndex:
     A   B
100  1  10
102  2  20
104  3  30
106  4  40
108  5  50

Key Properties

Property Description Example
start Starting value 10
stop Stopping value (exclusive) 30
step Step size 2
name Index name "data"

Conclusion

RangeIndex is a memory-efficient way to create sequential indices in Pandas. It's particularly useful for large datasets where you need regular, monotonic indices without storing each value individually.

Updated on: 2026-03-26T16:40:15+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements