pandas 如何找出列中唯一值的数量以及数据框中唯一值的数量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43066374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find out number of unique values in a column along with count of the unique values in a dataframe?
提问by Sidhartha
As per the following data set I want no get the number of unique values and count of the unique values.
根据以下数据集,我不希望获得唯一值的数量和唯一值的数量。
My data set:
我的数据集:
Account_Type
Gold
Gold
Platinum
Gold
Output :
输出 :
no of unique values : 2
unique values : [Gold,Platinum]
Gold : 3
Platinum :1
采纳答案by piRSquared
Use pd.value_counts
用 pd.value_counts
pd.value_counts(df.Account_Type)
Gold 3
Platinum 1
Name: Account_Type, dtype: int64
Get number of unique as well
获取唯一的数量
s = pd.value_counts(df.Account_Type)
s1 = pd.Series({'nunique': len(s), 'unique values': s.index.tolist()})
s.append(s1)
Gold 3
Platinum 1
nunique 2
unique values [Gold, Platinum]
dtype: object
回答by adabsurdum
You can use set()
to remove duplicates and then calculate the length:
您可以使用set()
删除重复项然后计算长度:
len(set(data_set))
len(set(data_set))
To count the occurrence:
要计算发生次数:
data_set.count(value)
data_set.count(value)
回答by missnomer
df['Account_Type].unique()
returns unique values of the specified column (in this case 'Account_Type') as a NumPy array.
以 NumPy 数组的形式返回指定列(在本例中为“Account_Type”)的唯一值。
All you have to do is use the len() function to find the no of unique values in the array.
您所要做的就是使用 len() 函数来查找数组中唯一值的个数。
len(df['Account_Type].unique())
To find the respective counts of unique values, you can use value_counts()
要查找唯一值的相应计数,您可以使用 value_counts()