Python Pandas - 返回 Index 对象中的唯一元素数
要返回 Index 对象中唯一元素的数量,请在 Pandas 中使用 index.nunique() 方法。首先,导入所需的库 −
import pandas as pd
创建 Pandas 索引 −
index = pd.Index([50, 10, 70, 110, 90, 50, 110, 90, 30])
显示 Pandas 索引 −
print("Pandas Index...\n",index)获取索引中唯一值的数目 −
print("\nCount of unique values...\n",index.nunique())
示例
下面是代码 −
import pandas as pd
# Creating Pandas index
index = pd.Index([50, 10, 70, 110, 90, 50, 110, 90, 30])
# 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 dtype of the data
print("\nThe dtype object...\n",index.dtype)
# Get the unique values from the index
# Unique values are returned in order of appearance, this does NOT sort
print("\nUnique values from the Index..\n", index.unique())
# Get the number of unique values in the index
print("\nCount of unique values...\n",index.nunique())输出
将生成以下输出 −
Pandas Index... Int64Index([50, 10, 70, 110, 90, 50, 110, 90, 30], dtype='int64') Number of elements in the index... 9 The dtype object... int64 Unique values from the Index.. Int64Index([50, 10, 70, 110, 90, 30], dtype='int64') Count of unique values... 6
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP