Python Pandas - 從分割陣列建立 IntervalArray 並檢查區間是左閉右開、左開右閉、左右都閉還是都不閉
要從分割陣列建立 IntervalArray,請使用 **pandas.arrays.IntervalArray.from_breaks()**。
要檢查區間是左閉右開、左開右閉、左右都閉還是都不閉,請使用 array.closed 屬性。
首先,匯入所需的庫:
import pandas as pd
從類似陣列的分割建立新的 IntervalArray。預設情況下,區間在“右側”閉合:
array = pd.arrays.IntervalArray.from_breaks([0, 1, 2, 3, 4, 5])
顯示區間:
print("Our IntervalArray...\n",array)檢查 IntervalArray 中的區間是左閉、右閉、左右都閉還是都不閉:
print("\nChecking whether the intervals is closed...\n",array.closed)
示例
以下是程式碼:
import pandas as pd
# Construct a new IntervalArray from an array-like of splits
# the intervals are closed on the "right" by default
array = pd.arrays.IntervalArray.from_breaks([0, 1, 2, 3, 4, 5])
# Display the IntervalArray
print("Our IntervalArray...\n",array)
# Getting the length of IntervalArray
# Returns an Index with entries denoting the length of each Interval in the IntervalArray
print("\nOur IntervalArray length...\n",array.length)
# midpoint of each Interval in the IntervalArray as an Index
print("\nThe midpoint of each interval in the IntervalArray...\n",array.mid)
# get the right endpoints
print("\nThe right endpoints of each Interval in the IntervalArray as an Index...\n",array.right)
# check whether the intervals in the Interval Array is closed on the left-side, right-side,
# both or neither
print("\nChecking whether the intervals is closed...\n",array.closed)輸出
這將產生以下程式碼:
Our IntervalArray... <IntervalArray> [(0, 1], (1, 2], (2, 3], (3, 4], (4, 5]] Length: 5, dtype: interval[int64, right] Our IntervalArray length... Int64Index([1, 1, 1, 1, 1], dtype='int64') The midpoint of each interval in the IntervalArray... Float64Index([0.5, 1.5, 2.5, 3.5, 4.5], dtype='float64') The right endpoints of each Interval in the IntervalArray as an Index... Int64Index([1, 2, 3, 4, 5], dtype='int64') Checking whether the intervals is closed... Right
廣告
資料結構
網路
關係型資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP