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


要計算與給定標籤相對應的左切片邊界,請使用index.get_slice_bound()。將side引數設定為left

首先,匯入所需的庫 -

import pandas as pd

建立 Pandas 索引 -

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

顯示 Pandas 索引 -

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

獲取左切片邊界。如果“side”引數設定為“left”,則返回給定標籤的左切片邊界

print("\nGet the left slice bound...\n",index.get_slice_bound(30, side='left', 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 left slice bound
# Returns the left slice bound of given label if "side" parameter is set to "left"
print("\nGet the left slice bound...\n", index.get_slice_bound(30, side='left', kind ='getitem'))

輸出

這將生成以下輸出 -

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

Number of elements in the index...
7

Get the left slice bound...
2

更新於: 14-10-2021

66 次瀏覽

開啟你的 事業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.