首页 文章资讯内容详情

Python Pandas - 计算对应于给定标签的左切片边界

2026-06-03 1 花语

要计算对应于给定标签的左切片边界,请使用.将side参数设置为leftindex.get_slice_bound()

首先,导入所需的库-

import pandas as pd

创建熊猫索引-

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

显示熊猫指数-

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 #创建Pandas索引 index = pd.Index([10, 20, 30, 40, 50, 60, 70]) #显示Pandas索引 print("Pandas Index...\n",index) #返回索引中的元素数 print("\nNumber of elements in the index...\n",index.size) #获取左切片边界 # Returns the left slice bound of given label if "side" 参数设置为 "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