The DataFrame object DataFrame not have nunique . You must choose in which column you want to apply nunique() . You can do this with a simple dot operator:
df.groupby('A').apply(lambda x: xBnunique())
will print:
A bar 2 flux 2 foo 3
And do:
df.groupby('A').apply(lambda x: xEnunique())
will print:
A bar 1 flux 2 foo 2
Alternatively, you can do this with a single function call, using:
df.groupby('A').aggregate({'B': lambda x: x.nunique(), 'E': lambda x: x.nunique()})
which will print:
BE A bar 2 1 flux 2 2 foo 3 2
To answer the question of why your recursive lambda prints column A , because when you do a groupby / apply operation, you now iterate through three DataFrame . Each DataFrame is a sub- DataFrame original. Applying operations to it will apply to each Series . There are three Series behind the DataFrame that you apply to the nunique() operator.
The first Series to be evaluated on each DataFrame is A Series , and since you made groupby on A , you know that each DataFrame has only one unique value in the A Series . This explains why you are ultimately assigned the result column A with all 1 .
source share