Python Pandas - Create an IntervalIndex

PythonServer Side ProgrammingProgramming

To create an IntervalIndex in Pandas, use the pandas.IntervalIndex.from_arrays() method. At first, import the required libraries −

import pandas as pd

Create IntervalIndex −

interval = pd.IntervalIndex.from_arrays([5, 10, 15], [10, 15, 20])

Display the interval −

print("IntervalIndex...\n",interval)

Example

Following is the code −

import pandas as pd

# Create IntervalIndex
interval = pd.IntervalIndex.from_arrays([5, 10, 15], [10, 15, 20])

# display the interval
print("IntervalIndex...\n",interval)

# display the interval length
print("\nIntervalIndex length...\n",interval.length)

Output

This will produce the following output −

IntervalIndex...
IntervalIndex([(5, 10], (10, 15], (15, 20]], dtype='interval[int64, right]')

IntervalIndex length...
Int64Index([5, 5, 5], dtype='int64')
raja
Updated on 18-Oct-2021 07:24:13

Advertisements