Pandas 'DataFrame' object has no attribute 'unique'

73,186

Solution 1

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

Solution 2

Rather than removing duplicates during the pivot table process, use the df.drop_duplicates() function to selectively drop duplicates.

For example if you are pivoting using these index='c0' and columns='c1' then this simple step yields the correct counts.

In this example the 5th row is a duplicate of the 4th (ignoring the non-pivoted c2 column

import pandas as pd
data = {'c0':[0,1,0,1,1], 'c1':[0,0,1,1,1], 'person':[0,0,1,1,1], 'c_other':[1,2,3,4,5]}
df = pd.DataFrame(data)
df2 = df.drop_duplicates(subset=['c0','c1','person'])
pd.pivot_table(df2, index='c0',columns='c1',values='person', aggfunc='count')

This correctly outputs

c1  0  1
c0      
0   1  1
1   1  1

Solution 3

One very easy solution to get the unique combinations of >1 columns from a DF is the following:

unique_A_B_combos = df[['A', 'B']].value_counts().index.values

Solution 4

df[['col1', 'col2']].nunique()

Try this instead of separate function

Share:
73,186
jwzinserl
Author by

jwzinserl

Updated on October 04, 2021

Comments

  • jwzinserl
    jwzinserl over 2 years

    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?

  • Eliethesaiyan
    Eliethesaiyan over 7 years
    what about df.describe,info? fillna?
  • Duck Ling
    Duck Ling about 2 years
    just a note: this returns a numpy array as opposed to a dataframe.