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

Python Pandas - Tính toán giới hạn lát bên phải tương ứng với nhãn đã cho

Để tính toán giới hạn lát bên phải tương ứng với nhãn đã cho, hãy sử dụng index.get_slice_bound () . Đặt bên tham số thành phải .

Đầu tiên, hãy nhập các thư viện được yêu cầu -

import pandas as pd

Tạo chỉ mục Pandas -

index = pd.Index([10, 20, 30, 40, 50, 60, 70])

Hiển thị chỉ mục Gấu trúc -

print("Pandas Index...\n",index)

Có được ràng buộc lát bên phải. Trả về giới hạn lát bên phải của nhãn đã cho nếu tham số "bên" được đặt thành "bên phải" -

print("\nGet the right slice bound...\n",index.get_slice_bound(30, side='right', kind ='getitem'))

Ví dụ

Sau đây là mã -

import pandas as pd

# Creating Pandas index
index = pd.Index([10, 20, 30, 40, 50, 60, 70])

# Display the Pandas index
print("Pandas Index...\n",index)

# Return the number of elements in the Index
print("\nNumber of elements in the index...\n",index.size)

# Get the right slice bound
# Returns the right slice bound of given label if "side" parameter is set to "right"
print("\nGet the right slice bound...\n", index.get_slice_bound(30, side='right', kind ='getitem'))

Đầu ra

Điều này sẽ tạo ra kết quả sau -

Pandas Index...
Int64Index([10, 20, 30, 40, 50, 60, 70], dtype='int64')

Number of elements in the index...
7

Get the right slice bound...
3