Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Python Pandas - ตรวจสอบว่าช่วงปิดอยู่ทางด้านขวาหรือไม่


ในการตรวจสอบว่า Interval ถูกปิดทางด้านซ้ายหรือไม่ ให้ใช้ interval.closed_right คุณสมบัติ. ขั้นแรก นำเข้าไลบรารีที่จำเป็น -

import pandas as pd

ช่วงเวลาที่กำหนดโดยใช้พารามิเตอร์ "ปิด" ที่มีค่า "right" เช่น [0, 5) อธิบายโดย 0

interval = pd.Interval(left=0, right=20, closed='right')

แสดงช่วงเวลา

print("Interval...\n",interval)

ตรวจสอบว่าช่วงปิดอยู่ทางด้านขวาหรือไม่

print("\nChecking whether the Interval is closed on the right...\n", interval.closed_right)

ตัวอย่าง

ต่อไปนี้เป็นรหัส

import pandas as pd

# Interval set using the "closed" parameter with value "right"
# i.e. [0, 5) is described by 0 < x <= 5 when closed='right'
interval = pd.Interval(left=0, right=20, closed='right')

# display the interval
print("Interval...\n",interval)

# display the interval length
print("\nInterval length...\n",interval.length)

# check whether the interval is closed on the right-side
print("\nChecking whether the Interval is closed on the right...\n", interval.closed_right)

# check for the existence of an element in an Interval
# This shows that closed = right contain only the right-most endpoint
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

Checking whether the Interval is closed on the right...
True