Python Pandas - 返回與當前相同的區間陣列,但在指定的一側閉合


要返回與當前相同的區間陣列,但在指定的一側閉合,請使用 **array.set_closed()** 並將引數設定為 **both**。

首先,匯入所需的庫 -

import pandas as pd

從類似陣列的分裂中構造一個新的 IntervalArray -

array = pd.arrays.IntervalArray.from_breaks([0, 1, 2, 3, 4, 5])

顯示區間 -

print("Our IntervalArray...\n",array)

與當前相同的區間陣列,但在指定的一側閉合 -

print("\nAn identical IntervalArray closed on the specified side...\n", array.set_closed('both'))

示例

以下是程式碼 -

import pandas as pd

# Construct a new IntervalArray from an array-like of splits
array = pd.arrays.IntervalArray.from_breaks([0, 1, 2, 3, 4, 5])

# Display the IntervalArray
print("Our IntervalArray...\n",array)

print("\nAn identical IntervalArray closed on the specified side...\n", array.set_closed('both'))

輸出

這將生成以下程式碼 -

Our IntervalArray...
<IntervalArray>
[(0, 1], (1, 2], (2, 3], (3, 4], (4, 5]]
Length: 5, dtype: interval[int64, right]

An identical IntervalArray closed on the specified side...
<IntervalArray>
[[0, 1], [1, 2], [2, 3], [3, 4], [4, 5]]
Length: 5, dtype: interval[int64, both]

更新於: 2021-10-13

71 次檢視

啟動你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.