Pandas 'DataFrame' object has no attribute 'unique'

thesebeth picture thesebeth · Mar 25, 2015 · Viewed 44.8k times · Source

I'm working in pandas doing pivot tables and when doing the groupby (to count distinct observations) aggfunc={"person":{lambda x: len(x.unique())}} gives me the following error: 'DataFrame' object has no attribute 'unique' any ideas how to fix it?

Answer

Alexander picture Alexander · Mar 25, 2015

DataFrames do not have that method; columns in DataFrames do:

df['A'].unique()

Or, to get the names with the number of observations (using the DataFrame given by closedloop):

>>> df.groupby('person').person.count()
Out[80]: 
person
0         2
1         3
Name: person, dtype: int64