Computer >> Máy Tính >  >> Lập trình >> Python

Python Pandas - Trả lại điểm giữa của Khoảng thời gian

Để trả về điểm giữa của Khoảng thời gian, hãy sử dụng khoảng thời gian.mid bất động sản. Đầu tiên, hãy nhập các thư viện được yêu cầu -

import pandas as pd

Khoảng thời gian mở được đặt bằng cách sử dụng tham số "đã đóng" với giá trị "không". Khoảng mở (trong toán học được ký hiệu bằng dấu ngoặc vuông) không chứa các điểm cuối của nó, tức là khoảng mở [0, 5] được đặc trưng bởi các điều kiện 0

interval = pd.Interval(5, 20, closed='neither')

Hiển thị khoảng thời gian

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

Trả về điểm giữa của khoảng thời gian

print("\nThe midpoint for the Interval...\n",interval.mid)

Ví dụ

Sau đây là mã

import pandas as pd

# Open interval set using the "closed" parameter with value "neither"
# An open interval (in mathematics denoted by square brackets) does not contains its endpoints,
# i.e. the open interval [0, 5] is characterized by the conditions 0 < x < 5.
interval = pd.Interval(5, 20, closed='neither')

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

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

# the left bound
print("\nThe left bound for the Interval...\n",interval.left)

# the right bound
print("\nThe right bound for the Interval...\n",interval.right)

# return the midpoint of the Interval
print("\nThe midpoint for the Interval...\n",interval.mid)

Đầu ra

Điều này sẽ tạo ra mã sau

Interval...
(5, 20)

Interval length...
15

The left bound for the Interval...
5

The right bound for the Interval...
20

The midpoint for the Interval...
12.5