Python Pandas - 从索引中返回标签或如果不存在,则返回前一个
要从索引中返回标签或如果不存在,则返回前一个,在 Pandas 中使用 index.asof() 方法。
首先,导入必需的库 −
import pandas as pd
创建 Pandas 索引 −
index = pd.Index([10, 20, 30, 40, 50, 60, 70])
显示 Pandas 索引 −
print("Pandas Index...\n",index)从索引中返回标签或如果不存在,则返回前一个 −
print("\nGet the label from the index...\n",index.asof(43))
示例
以下为代码 −
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)
# Return the label from the index or if not present, the previous one
print("\nGet the label from the index...\n",index.asof(43))输出
这将产生以下输出 −
Pandas Index... Int64Index([10, 20, 30, 40, 50, 60, 70], dtype='int64') Number of elements in the index... 7 Get the label from the index... 40
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 程序设计
C++
C#
MongoDB
MySQL
Javascript
PHP