Python| Pandas Index.unique()
Python是一种用于进行数据分析的出色语言,主要是因为以数据为中心的Python包的奇妙生态系统。 Pandas就是其中之一,它使导入和分析数据变得更加容易。
Pandas Index.unique()
函数返回索引中的唯一值。唯一值按出现顺序返回,这不会排序。
Syntax: Index.unique(level=None)
Parameters :
level : Only return values from specified level (for MultiIndex)
Returns : Index without duplicates
示例 #1:使用Index.unique()
函数返回索引中的唯一值。
# importing pandas as pd
import pandas as pd
# Creating the index
idx = pd.Index(['Harry', 'Mike', 'Arther', 'Nick',
'Harry', 'Arther'], name ='Student')
# Print the Index
print(idx)
输出 :
让我们找到索引中的所有唯一值。
# find unique values in the index
idx.unique()
输出 :
该函数返回了一个新索引,该索引具有原始索引的所有唯一值。示例 #2:使用Index.unique()
函数查找索引中的唯一值
# importing pandas as pd
import pandas as pd
# Creating the index
idx = pd.Index([21, 10, 30, 40, 50, 10, 50])
# Print the Index
print(idx)
输出 :
让我们找到索引中的所有唯一值
# for finding the unique values in the index
idx.unique()
输出 :
该函数返回了一个新索引,其中包含原始索引中的所有唯一值。