Python Pandas - 計算與給定標籤相對應的正確切片邊界


使用index.get_slice_bound()計算與給定標籤相對應的正確切片邊界。將side引數設定為right

首先,匯入所需的庫 --

import pandas as pd

建立 Pandas 索引 --

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

顯示 Pandas 索引 --

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

獲取正確的切片邊界。如果將 “side” 引數設定為 “right”,則返回給定標籤的正確切片邊界 --

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

例項

以下為程式碼 --

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'))

輸出

這將輸出以下內容 --

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

更新於:2021-10-14

146 次瀏覽

啟動你的 職業

透過完成課程獲取認證

開始
廣告
© . All rights reserved.