Python是進行數據分析的一種出色語言,主要是因為以數據為中心的python軟件包具有奇妙的生態係統。 Pandas是其中的一種,使導入和分析數據更加容易。
Pandas dataframe.nunique()
函數返回在請求的軸上具有不同觀測值數量的係列。如果將axis的值設置為0,則它將找到索引軸上唯一觀測值的總數。如果將axis的值設置為1,則它將找到列軸上唯一觀測值的總數。它還提供了排除NaN
唯一數字計數得到的值。
用法: DataFrame.nunique(axis=0, dropna=True)
參數:
axis:{0或“索引”,1或“列”},默認0
dropna:計數中不要包含NaN。
返回:nunique:係列
範例1:采用nunique()
函數查找列軸上唯一值的數量。
# importing pandas as pd
import pandas as pd
# Creating the first dataframe
df = pd.DataFrame({"A":[14, 4, 5, 4, 1],
"B":[5, 2, 54, 3, 2],
"C":[20, 20, 7, 3, 8],
"D":[14, 3, 6, 2, 6]})
# Print the dataframe
df
讓我們使用dataframe.nunique()
函數來查找整個列軸上的唯一值。
# find unique values
df.nunique(axis = 1)
輸出:
正如我們在輸出中看到的那樣,該函數將打印總編號。每行的唯一值的數量。
範例2:采用nunique()
函數查找 DataFrame 中索引軸上唯一值的數量。 DataFrame 包含NaN
值。
# importing pandas as pd
import pandas as pd
# Creating the first dataframe
df = pd.DataFrame({"A":["Sandy", "alex", "brook", "kelly", np.nan],
"B":[np.nan, "olivia", "olivia", "", "amanda"],
"C":[20 + 5j, 20 + 5j, 7, None, 8],
"D":[14.8, 3, None, 6, 6]})
# apply the nunique() function
df.nunique(axis = 0, dropna = True)
輸出:
該函數將空字符串視為第2列中的唯一值。
相關用法
- 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 dataframe.nunique()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。