要返回左、右形式的元组 ndarray,请使用to_tuples()Pandas 中的方法。首先,导入所需的库 -
import pandas as pd
创建 IntervalArray -
index = pd.arrays.IntervalArray.from_breaks(range(5))
显示间隔 -
print("IntervalIndex...\n",index)
返回元组的 ndarray -
print("\nThe ndarray of Tuples...\n",index.to_tuples())
以下是代码 -
import pandas as pd # 创建间隔数组 index = pd.arrays.IntervalArray.from_breaks(range(5)) # 显示间隔 print("IntervalIndex...\n",index) # 显示间隔 length print("\nIntervalIndex length...\n",index.length) # 返回元组的 ndarray print("\nThe ndarray of Tuples...\n",index.to_tuples())输出结果
这将产生以下输出 -
IntervalIndex... <IntervalArray> [(0, 1], (1, 2], (2, 3], (3, 4]] Length: 4, dtype: interval[int64, right] IntervalIndex length... Int64Index([1, 1, 1, 1], dtype='int64') The ndarray of Tuples... [(0, 1) (1, 2) (2, 3) (3, 4)]