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

Python Pandas - Lấy điểm giữa từ IntervalIndex

Để lấy điểm giữa từ IntervalIndex, hãy sử dụng thuộc tính period.mid trong Pandas. Đầu tiên, hãy nhập các thư viện được yêu cầu -

import pandas as pd

Tạo IntervalIndex -

interval = pd.IntervalIndex.from_arrays([10, 15, 20], [20, 25, 30])

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

print("IntervalIndex...\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

# Create IntervalIndex
interval = pd.IntervalIndex.from_arrays([10, 15, 20], [20, 25, 30])

# Display the interval
print("IntervalIndex...\n",interval)

# Display the interval length
print("\nIntervalIndex length...\n",interval.length)

# Check whether the IntervalIndex is closed on the left-side, right-side, both or neither
print("\nChecking for the type of IntervalIndex...\n",interval.closed)

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

# the right bound
print("\nThe right bound for the IntervalIndex...\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 kết quả sau -

IntervalIndex...
IntervalIndex([(10, 20], (15, 25], (20, 30]], dtype='interval[int64, right]')

IntervalIndex length...
Int64Index([10, 10, 10], dtype='int64')

Checking for the type of IntervalIndex...
right

The left bound for the IntervalIndex...
Int64Index([10, 15, 20], dtype='int64')

The right bound for the IntervalIndex...
Int64Index([20, 25, 30], dtype='int64')

The midpoint for the Interval...
Float64Index([15.0, 20.0, 25.0], dtype='float64')