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
Answer from Alexander on Stack Overflow
🌐
GitHub
github.com › pandas-dev › pandas › issues › 11640
BUG AttributeError: 'DataFrameGroupBy' object has no attribute '_obj_with_exclusions' · Issue #11640 · pandas-dev/pandas
November 18, 2015 - In [5]: df.groupby('a').mean() --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-29-a830c6135818> in <module>() ----> 1 df.groupby('a').mean() /home/nicolas/Git/pandas/pandas/core/groupby.py in mean(self) 764 self._set_selection_from_grouper() 765 f = lambda x: x.mean(axis=self.axis) --> 766 return self._python_agg_general(f) 767 768 def median(self): /home/nicolas/Git/pandas/pandas/core/groupby.py in _python_agg_general(self, func, *args, **kwargs) 1245 output[name] = self._try_cast(values[mask], result)
Author   nbonnotte
Discussions

python - Error 'AttributeError: 'DataFrameGroupBy' object has no attribute' while groupby functionality on dataframe - Stack Overflow
The problem is it not identifying the NEWS_SENTIMENT_DAILY_AVG column. Error message - AttributeError: 'DataFrameGroupBy' object has no attribute 'NEWS_SENTIMENT_DAILY_AVG' More on stackoverflow.com
🌐 stackoverflow.com
python - Number of unique values per column by group - Stack Overflow
Consider the following dataframe: A B E 0 bar one 1 1 bar three 1 2 flux six 1 3 flux three 2 4 foo five 2 5 foo one 1 6 foo two 1 7 foo two 2 I More on stackoverflow.com
🌐 stackoverflow.com
Pandas unique doest not work on groupby object when applied on several columns - Stack Overflow
Lets' say I have a dataframe with 3 columns, one containing the groups, and I would to collect the collections of values in the 2 other columns for each group. Normally I would use the pandas.groupby More on stackoverflow.com
🌐 stackoverflow.com
python - AttributeError: 'DataFrame' object has no attribute 'group_by' - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 2
12

The DataFrame object doesn't have nunique, only Series do. You have to pick out which column you want to apply nunique() on. You can do this with a simple dot operator:

df.groupby('A').apply(lambda x: x.B.nunique())

will print:

A
bar     2
flux    2
foo     3

And doing:

df.groupby('A').apply(lambda x: x.E.nunique())

will print:

A
bar     1
flux    2
foo     2

Alternatively you can do this with one function call using:

df.groupby('A').aggregate({'B': lambda x: x.nunique(), 'E': lambda x: x.nunique()})

which will print:

      B  E
A
bar   2  1
flux  2  2
foo   3  2

To answer your question about why your recursive lambda prints the A column as well, it's because when you do a groupby/apply operation, you're now iterating through three DataFrame objects. Each DataFrame object is a sub-DataFrame of the original. Applying an operation to that will apply it to each Series. There are three Series per DataFrame you're applying the nunique() operator to.

The first Series being evaluated on each DataFrame is the A Series, and since you've done a groupby on A, you know that in each DataFrame, there is only one unique value in the A Series. This explains why you're ultimately given an A result column with all 1's.

2 of 2
3

I encountered the same problem. Upgrading pandas to the latest version solved the problem for me.

df.groupby('A').nunique()

The above code did not work for me in Pandas version 0.19.2. I upgraded it to Pandas version 0.21.1 and it worked.

You can check the version using the following code:

print('Pandas version ' + pd.__version__)
🌐
Itsourcecode
itsourcecode.com › home › attributeerror: ‘dataframe’ object has no attribute ‘unique’
Attributeerror: 'dataframe' object has no attribute 'unique'
April 3, 2023 - In addition, Python is a high-level ... attributeerror: 'dataframe' object has no attribute 'unique', you have to replace the unique() method with an appropriate one....
Find elsewhere
🌐
Data Science Learner
datasciencelearner.com › attributeerror-dataframe-object-has-no-attribute-unique-solved
AttributeError: 'dataframe' object has no attribute 'unique' ( Solved )
September 12, 2023 - The solution to this AttributeError is that you have to use a unique() attribute on a specific column, not the entire dataframe.
🌐
Reddit
reddit.com › r/learnpython › pandas attributeerror: 'dataframe' object has no attribute 'group_by'
r/learnpython on Reddit: Pandas AttributeError: 'DataFrame' object has no attribute 'group_by'
February 28, 2018 -

Hello,

Has anyone ever come across this before?

I'm trying to group some data in a dataframe and getting this error. The steps I've taken are:

  1. in a for loop:

read in a csv from an api using pd.read_csv() replaced some values in a column using a for loop and .loc[] appended the resulting data frame to a list

2) concatenated the list of dataframes using pd.concat()

3) added a calculated column to the new DF by multiplying another column

4) added two empty columns

5) filtered the DF using .loc[] based on a value within a column

6) filtered the DF using .loc[] based on a value in a different column

7) tried to use this code:

new_DF = old_df.group_by(['col1', 'col_2', 'col_3', 'adgroup', 'col_4', 'col5', 'col6'], as_index=False)[['col7', 'col8', 
'col9']].sum()

The DF seems to behaving normally for example I can do dtypes and columns on it and add columns which are calculated from other columns. What is super frustrating is that I can do pd.to_csv() and then pd.read_csv() on the DF and then I'm able to do the grouping I want (however this isn't ideal which is why I'm posting).

Any advice would be appreciated.

Cheers

🌐
GitHub
github.com › rapidsai › cuml › issues › 5811
[BUG] AttributeError: 'DataFrame' object has no attribute 'unique' · Issue #5811 · rapidsai/cuml
March 20, 2024 - Method of cuDF & cuML install: [conda, Docker, or from source] !pip install cudf-cu11 dask-cudf-cu11 cuml-cu11 cugraph-cu11 --extra-index-url=https://pypi.ngc.nvidia.com !rm -rf /usr/local/lib/python3.8/dist-packages/cupy* !pip install cupy-cuda11x
Author   Nithish-Chowdary
🌐
GitHub
github.com › rapidsai › cuml › issues › 3381
AttributeError: 'DataFrame' object has no attribute 'unique' · Issue #3381 · rapidsai/cuml
January 18, 2021 - This gives - AttributeError: 'DataFrame' object has no attribute 'unique' I also tried converting dask dataframe to dask array curf_model.fit(X_dask_cudf.to_dask_array(), y_dask_cudf.to_dask_array()) ... Dask / cuml.daskIssue/PR related to Python level dask or cuml.dask features.Issue/PR related to Python level dask or cuml.dask features.bugSomething isn't workingSomething isn't working
Author   dsouzavijeth
🌐
Nickmccullum
nickmccullum.com › advanced-python › pandas-common-operations
Common Operations in Pandas | Nick McCullum
df.unique() #Returns AttributeError: 'DataFrame' object has no attribute 'unique'
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 1.1 › reference › groupby.html
GroupBy — pandas 1.1.5 documentation
GroupBy objects are returned by groupby calls: pandas.DataFrame.groupby(), pandas.Series.groupby(), etc. The following methods are available in both SeriesGroupBy and DataFrameGroupBy objects, but may differ slightly, usually in that the DataFrameGroupBy version usually permits the specification of an axis argument, and often an argument indicating whether to restrict application to columns of a specific data type.
🌐
Databricks Community
community.databricks.com › t5 › data-engineering › issue-with-pyspark-groupby-groupeddata › td-p › 7255
Issue with Pyspark GroupBy GroupedData - Databricks Community - 7255
March 27, 2023 - The issue with your code is that the groupBy operation returns a GroupedData object, which does not have a get_group method. Instead, you can use the filter method to filter the bronze_df DataFrame for each entity name and write the resulting DataFrames to separate Silver tables.
🌐
Databricks Community
community.databricks.com › t5 › data-engineering › attributeerror-dataframe-object-has-no-attribute › td-p › 61132
AttributeError: 'DataFrame' object has no attribut... - Databricks Community - 61132
February 19, 2024 - Hello, I have some trouble deduplicating rows on the "id" column, with the method "dropDuplicatesWithinWatermark" in a pipeline. When I run this pipeline, I get the error message: "AttributeError: 'DataFrame' object has no attribute 'dropDuplicatesWithinWatermark'" Here is part of the code: @dl...
🌐
Saturn Cloud
saturncloud.io › blog › solving-the-attributeerror-cannot-access-callable-attribute-groupby-of-dataframegroupby-objects-in-pandas
Solving the AttributeError: Cannot Access Callable Attribute 'groupby' of 'DataFrameGroupBy' Objects in Pandas | Saturn Cloud Blog
September 11, 2023 - Pandas is a powerful data manipulation library in Python, widely used by data scientists for its robustness and versatility. However, it's not uncommon to encounter errors while working with it. One such error is the AttributeError: Cannot access callable attribute 'groupby' of 'DataFrameGroupBy' ...
🌐
Reddit
reddit.com › r/learnpython › pandas groupby error in function only?
r/learnpython on Reddit: Pandas Groupby Error in Function Only?
March 11, 2020 -

I get the error " AttributeError: 'str' object has no attribute 'groupby' " when I run the following piece of code:

def groupby(df):

median_df = df.groupby('Brand').median()

return median_df

median_customer_df = groupby('customer_df')

However when outside of the function I simply run:

median_df = customer_df.groupby('Brand').median()

the code runs fine?

Can anyone please help explain why this groupby is failing only when in my function?

Thanks in advance!