Python Pandas - Create RangeIndex from a range object


To create RangeIndex from a range object, use the pd.RangeIndex.from_range(range()) method in Pandas.

At first, import the required libraries −

import pandas as pd

Create RangeIndex −

index = pd.RangeIndex.from_range(range(10, 30))

Display the RangeIndex −

print("RangeIndex...\n",index)

Example

Following is the code −

import pandas as pd

# RangeIndex
index = pd.RangeIndex.from_range(range(10, 30))

# Display the RangeIndex
print("RangeIndex...\n",index)

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

# Display the end value
print("\nRangeIndex end value...\n",index.stop)

Output

This will produce the following output −

RangeIndex...
RangeIndex(start=10, stop=30, step=1)

RangeIndex start value...
10

RangeIndex end value...
30

Updated on: 14-Oct-2021

656 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements