Python Pandas - 返回索引中的唯一值
pythonserver side programmingprogramming更新于 2025/12/29 11:24:17
要返回索引中的唯一值,请使用 Pandas 中的 index.unique() 方法。首先,导入所需的库 -
import pandas as pd
创建 Pandas 索引 −
index = pd.Index([10, 50, 70, 10, 90, 50, 10, 30])
显示 Pandas 索引 −
print("Pandas Index...\n",index)
从索引中获取唯一值。按出现顺序返回唯一值,这不会对 − 进行排序
index.unique()
示例
以下是代码 −
import pandas as pd
# 创建 Pandas 索引
index = pd.Index([10, 50, 70, 10, 90, 50, 10, 30])
# 显示 Pandas 索引
print("Pandas Index...\n",index)
# 返回索引中的元素数量
print("\n索引中的元素数量...\n",index.size)
# 返回数据的 dtype
print("\ndtype对象...\n",index.dtype)
# 从索引中获取唯一值
# 唯一值按出现顺序返回,不进行排序
print("\nUnique values from the Index..\n", index.unique())
输出
这将产生以下输出 −
Pandas Index... Int64Index([10, 50, 70, 10, 90, 50, 10, 30], dtype='int64') 索引中的元素数量... 8 dtype 对象... int64 Unique values from the Index.. Int64Index([10, 50, 70, 90, 30], dtype='int64')
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

