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

[BUG] AttributeError: 'DataFrame' object has no attribute 'unique'
There was an error while loading. Please reload this page · Describe the bug I'm trying to build a Random Forest Regressor using cuml.dask.ensemble.RandomForestRegressor. collab V100 GPUs are used More on github.com
🌐 github.com
4
March 20, 2024
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
AttributeError: 'DataFrame' object has no attribute 'unique'
This gives - AttributeError: 'DataFrame' object has no attribute 'unique' More on github.com
🌐 github.com
4
January 18, 2021
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
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__)
🌐
GitHub
github.com › rapidsai › cuml › issues › 5811
[BUG] AttributeError: 'DataFrame' object has no attribute 'unique' · Issue #5811 · rapidsai/cuml
March 20, 2024 - [BUG] AttributeError: 'DataFrame' object has no attribute 'unique' #5811 · Copy link · Labels · ? - Needs TriageNeed team to review and classifyNeed team to review and classifybugSomething isn't workingSomething isn't working · Nithish-Chowdary · opened · on Mar 20, 2024 ·
Author   Nithish-Chowdary
🌐
Data Science Learner
datasciencelearner.com › attributeerror-dataframe-object-has-no-attribute-unique-solved
AttributeError: 'dataframe' object has no attribute 'unique' ( Solved )
September 12, 2023 - If you want to manipulate any datasets ... for achieving that task. If you are using the unique() attribute on dataframe then you may encounter the error '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.
Find elsewhere
🌐
Itsourcecode
itsourcecode.com › home › attributeerror: ‘dataframe’ object has no attribute ‘unique’
Attributeerror: 'dataframe' object has no attribute 'unique'
April 3, 2023 - To solve the error attributeerror: 'dataframe' object has no attribute 'unique', you have to replace the unique() method with an appropriate one.
🌐
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'
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'
🌐
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...
🌐
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

🌐
Kaggle
kaggle.com › questions-and-answers › 511815
Convert DataFrameGroupBy object to a DataFrame
Checking your browser before accessing www.kaggle.com · Click here if you are not automatically redirected after 5 seconds
🌐
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.
🌐
Quora
quora.com › How-do-you-fix-pandas-that-have-no-attribute-dataframe
How to fix pandas that have no attribute dataframe - Quora
Coding, Deep Learning, Rockets ... want to try the code below. ... This happens if you are trying to access some object with an attribute, and that attribute doesn't exist or mis-spelled....
🌐
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 - In the above code, the DataFrame df is grouped by both ‘A’ and ‘B’ columns. This will not raise any error. The AttributeError: Cannot access callable attribute 'groupby' of 'DataFrameGroupBy' objects is a common error encountered while working with Pandas.