Python是進行數據分析的一種出色語言,主要是因為以數據為中心的python軟件包具有奇妙的生態係統。 Pandas是其中的一種,使導入和分析數據更加容易。
Pandas Index.nunique()
函數返回對象中唯一元素的數量。它返回一個標量值,它是Index中所有唯一值的計數。默認情況下NaN
值不包括在計數中。如果dropna參數設置為False
然後包括NaN
計數中的值。
用法: Index.nunique(dropna=True)
參數:
dropna:計數中不包括NaN。
返回:nunique:int
範例1:采用Index.nunique()()
函數在索引中查找唯一值的計數。不包括NaN
計數中的值。
# importing pandas as pd
import pandas as pd
# Creating the index
idx = pd.Index(['Beagle', 'Pug', 'Labrador', 'Pug',
'Mastiff', None, 'Beagle'])
# Print the Index
idx
輸出:
讓我們在索引中找到唯一值的計數。
# to find the count of unique values.
idx.nunique(dropna = True)
輸出:
正如我們在輸出中看到的那樣,該函數返回了4,表示索引中隻有4個唯一值。
範例2:采用Index.nunique()
函數查找索引中的所有唯一值。還包括缺失值,即NaN
計數中的值。
# importing pandas as pd
import pandas as pd
# Creating the index
idx = pd.Index(['Beagle', 'Pug', 'Labrador', 'Pug',
'Mastiff', None, 'Beagle'])
# Print the Index
idx
輸出:
讓我們在索引中找到唯一值的計數。
# to find the count of unique values.
idx.nunique(dropna = False)
輸出:
正如我們在輸出中看到的那樣,該函數已返回5,指示Index中隻有5個唯一值。我們還將缺失值包括在計數中。
相關用法
- Python pandas.map()用法及代碼示例
- Python Pandas Series.str.len()用法及代碼示例
- Python Pandas.factorize()用法及代碼示例
- Python Pandas TimedeltaIndex.name用法及代碼示例
- Python Pandas dataframe.ne()用法及代碼示例
- Python Pandas Series.between()用法及代碼示例
- Python Pandas DataFrame.where()用法及代碼示例
- Python Pandas Series.add()用法及代碼示例
- Python Pandas.pivot_table()用法及代碼示例
- Python Pandas Series.mod()用法及代碼示例
- Python Pandas Dataframe.at[ ]用法及代碼示例
- Python Pandas Dataframe.iat[ ]用法及代碼示例
- Python Pandas.pivot()用法及代碼示例
- Python Pandas dataframe.mul()用法及代碼示例
- Python Pandas.melt()用法及代碼示例
注:本文由純淨天空篩選整理自Shubham__Ranjan大神的英文原創作品 Python | Pandas Index.nunique()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。