简体   繁体   English

Python Pandas获得独特的列数

[英]Python Pandas Get Unique Count of Column

I'm new to Python, lots of JS experience and SQL, and everything I've found so far is counting number of uniques for each value, but all I want is a count of unique values in the column "Team" 我是Python的新手,有很多JS经验和SQL,到目前为止我发现的所有东西都在计算每个值的唯一值,但我想要的只是“团队”列中的唯一值计数

uniqueCount= teamTable.groupby('Team').nunique()
print uniqueCount

But this only gives me the error "AttributeError: 'DataFrameGroupBy' object has no attribute 'nunique'" 但这只给我错误“AttributeError:'DataFrameGroupBy'对象没有属性'nunique'”

Thought this would be simple enough to find, but honestly can't find the function 以为这很容易找到,但老实说找不到这个功能

Date        ID  Team
1/1/2017    A   Bears
1/1/2017    A   Packers
1/1/2017    A   Lions
1/1/2017    B   Lions
1/3/2017    A   Packers

你可以用:

len(df.Team.unique())

You can use Series.nunique : 你可以使用Series.nunique

print (df.Team.nunique())
3

Using np.unique 使用np.unique

np.unique(df.Team.values).size

3

在此输入图像描述


Followup to @Jeff's comment. 跟进@ Jeff的评论。 He was spot on, @Bernies answer scales the best with my crude test. 他很有见,@ Bernies的回答是我最粗略的测试。 Might be indistinguishable form @jezrael's over all problem types. 在所有问题类型上,@ jezrael可能无法区分。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python Pandas 基于另一列的唯一列值计数 - Python Pandas Count of unique column values based on another column 从 Python 中的 pandas 列中的每一行获取唯一字数 - Getting the unique word count from each row in a pandas column in Python Python Pandas Groupby 计算单个列中的唯一记录 - Python Pandas Groupby to count unique records in a single column pandas - groupby 多列并获得其他列的唯一计数 - pandas - groupby multiple columns and get unique count of other column 如何获取 pandas 中每对唯一列的列值计数? - How to get count of column values for each unique pair of columns in pandas? 熊猫-计算并从一列中获取唯一的字符串值 - Pandas - Count and get unique occurrences of string values from a column Python pandas:如果第 2 列不包含“字符串”,则从第 1 列获取唯一 ID - Python pandas: get unique id from column 1 if "string" not contains on column 2 python - 如何在python pandas中分组并取一列的计数除以数据框第二列的唯一计数? - How to do group by and take Count of one column divide by count of unique of second column of data frame in python pandas? 获取熊猫数据框中唯一值的计数 - Get count of count of unique values in pandas dataframe 从python数据框中的文本列中获取唯一时间戳的计数 - Get the count of Unique time stamps from a text column in a dataframe in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM