Python Pandas - 创建一个 IntervalIndex
要在Pandas中创建IntervalIndex,请使用pandas.IntervalIndex.from_arrays()方法。首先,导入所需的库-
import pandas as pd
创建间隔索引-
interval = pd.IntervalIndex.from_arrays([5, 10, 15], [10, 15, 20])
显示间隔-
print("IntervalIndex...\n",interval)
示例
以下是代码-
import pandas as pd #创建间隔索引 interval = pd.IntervalIndex.from_arrays([5, 10, 15], [10, 15, 20]) #显示间隔 print("IntervalIndex...\n",interval) #显示间隔 length print("\nIntervalIndex length...\n",interval.length)输出结果
这将产生以下输出-
IntervalIndex... IntervalIndex([(5, 10], (10, 15], (15, 20]], dtype='interval[int64, right]') IntervalIndex length... Int64Index([5, 5, 5], dtype='int64')