Python Pandas - 建立閉區間並檢查端點的存在
要建立閉區間,請使用 **pandas.Interval()** 並設定 closed 引數。要檢查兩個端點的存在,請使用 in 屬性。
首先,匯入所需的庫:
import pandas as pd
使用值為“both”的“closed”引數設定閉區間。閉區間(在數學中用方括號表示)包含其端點,例如閉區間 [0, 5] 的特徵是 0 <= x <= 5
interval = pd.Interval(left=0, right=20, closed='both')
顯示區間
print("Interval...\n",interval)檢查區間中是否存在元素。這表明 closed = both 包含其端點
print("\nThe left-most element exists in the Interval? = \n",0 in interval)
print("\nThe right-most element exists in the Interval? = \n",20 in interval)示例
以下是程式碼:
import pandas as pd
# Closed interval set using the "closed" parameter with value "both"
# A closed interval (in mathematics denoted by square brackets) contains its endpoints,
# i.e. the closed interval [0, 5] is characterized by the conditions 0 <= x <= 5.
interval = pd.Interval(left=0, right=20, closed='both')
# display the interval
print("Interval...\n",interval)
# display the interval length
print("\nInterval length...\n",interval.length)
# check for the existence of an element in an Interval
# This shows that closed = both contains its endpoints
print("\nThe left-most element exists in the Interval? = \n",0 in interval)
print("\nThe right-most element exists in the Interval? = \n",20 in interval)輸出
這將產生以下程式碼:
Interval... [0, 20] Interval length... 20
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP