Python Pandas IntervalIndex - 返回形式为 (left, right) 的元组 ndarray


若要返回形式为 left, right 的元组 ndarray,请在 Pandas 中使用 to_tuples() 方法。首先,导入所需的库 −

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

# Create IntervalArray
index = pd.arrays.IntervalArray.from_breaks(range(5))

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

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

# Return the ndarray of Tuples
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)]

更新于: 19-10-2021

162 次浏览

开启你的 事业

完成学习后获得认证

开始
广告