TheIntrepidSpiff
TheIntrepidSpiff

Reputation: 189

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"

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

But this only gives me the error "AttributeError: 'DataFrameGroupBy' object has no attribute '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

Upvotes: 2

Views: 2293

Answers (3)

piRSquared
piRSquared

Reputation: 294258

Using np.unique

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

3

enter image description here


Followup to @Jeff's comment. He was spot on, @Bernies answer scales the best with my crude test. Might be indistinguishable form @jezrael's over all problem types.

enter image description here

Upvotes: 3

jezrael
jezrael

Reputation: 862611

You can use Series.nunique:

print (df.Team.nunique())
3

Upvotes: 3

mechanical_meat
mechanical_meat

Reputation: 169304

You can get that with:

len(df.Team.unique())

Upvotes: 4

Related Questions