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
广告