Python Pandas - 创建 IntervalIndex
要在 Pandas 中创建 IntervalIndex,请使用 pandas.IntervalIndex.from_arrays() 方法。首先,导入所需的库 -
import pandas as pd
创建 IntervalIndex -
interval = pd.IntervalIndex.from_arrays([5, 10, 15], [10, 15, 20])
显示区间 -
print("IntervalIndex...\n",interval)
示例
以下为代码 -
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)
输出
这会产生以下输出 -
IntervalIndex... IntervalIndex([(5, 10], (10, 15], (15, 20]], dtype='interval[int64, right]') IntervalIndex length... Int64Index([5, 5, 5], dtype='int64')
广告