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
🌐
Nickmccullum
nickmccullum.com › advanced-python › pandas-common-operations
Common Operations in Pandas | Nick McCullum
df.unique() #Returns AttributeError: 'DataFrame' object has no attribute 'unique'
Discussions

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
dask xgboost AttributeError: 'DataFrame' object has no attribute 'unique'
I am trying to do classification using dask xgboost but get --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) in 2 client... More on github.com
🌐 github.com
3
July 15, 2022
[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
Why do I get error in Python- Pandas, AttributeError:: 'DataFrame' object has no attribute 'unique'? - Stack Overflow
I have some code to convert all columns of my DataFrame of type 'object' to type 'category'. I'm just looping through my DF by column, which has already been filtered to object types, and getting ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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
🌐
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 › dask › dask › issues › 9281
dask xgboost AttributeError: 'DataFrame' object has no attribute 'unique' · Issue #9281 · dask/dask
July 15, 2022 - --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-60-d22c31ae208b> in <module> 2 client = Client() 3 ----> 4 est.fit(ttx, tty) /opt/conda/lib/python3.8/site-packages/dask_xgboost/core.py in fit(self, X, y, classes, eval_set, sample_weight, sample_weight_eval_set, eval_metric, early_stopping_rounds) 563 classes = da.unique(y) 564 else: --> 565 classes = y.unique() 566 classes = classes.compute() 567 else: /opt/conda/lib/python3.8/site-packages/dask/dataframe/core.py in __getattr__(self, key) 3912 return self[key] 3913 else: -> 3914 raise AttributeError("'DataFrame' object has no attribute %r" % key) 3915 3916 def __dir__(self): AttributeError: 'DataFrame' object has no attribute 'unique'
Author   naarkhoo
🌐
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
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 59256050 › why-do-i-get-error-in-python-pandas-attributeerror-dataframe-object-has-no
Why do I get error in Python- Pandas, AttributeError:: 'DataFrame' object has no attribute 'unique'? - Stack Overflow
converted_obj = pd.DataFrame() for col in df201911_obj.columns: num_unique_values = len(df201911_obj[col].unique()) num_total_values = len(df201911_obj[col]) if num_unique_values / num_total_values < 0.5: converted_obj.loc[:,col] = df201911_obj[col].astype('category') else: converted_obj.loc[:,col] = df201911_obj[col] I am getting an AttributeError: 'DataFrame' object has no attribute 'unique'
🌐
Data Science Learner
datasciencelearner.com › python-exceptions › attributeerror › attributeerror-dataframe-object-has-no-attribute-unique-solved
AttributeError: 'dataframe' object has no attribute 'unique' ( Solved )
September 12, 2023 - You will get the ‘dataframe’ ... ... The solution to this AttributeError is that you have to use a unique() attribute on a specific column, not the entire dataframe....
🌐
Plain English
python.plainenglish.io › how-to-fix-attributeerror-in-python-6cea86059a27
How to Fix AttributeError in Python? | by JOKEN VILLANUEVA | Python in Plain English
January 27, 2025 - The error message “‘DataFrame’ object has no attribute ‘withColumn’” occurs when you are trying to add a new column to a Pandas DataFrame using the withColumn() method.
🌐
Medium
medium.com › @pies052022 › attributeerror-dataframe-object-has-no-attribute-unique-solved-b0b39050741f
AttributeError: ‘dataframe’ object has no attribute ‘unique’ [SOLVED] | by JOKEN VILLANUEVA | Medium
January 29, 2025 - To solve the error attributeerror: ‘dataframe’ object has no attribute ‘unique’, you have to replace the unique() method with an appropriate one.
🌐
Reddit
reddit.com › r/learnpython › "'dataframe' object has no attribute" issue
r/learnpython on Reddit: "'DataFrame' object has no attribute" Issue
October 30, 2020 -

I am in university and am taking a special topics class regarding AI. I have zero knowledge about Python, how it works, or what anything means.

A project for the class involves manipulating Bayesian networks to predict how many and which individuals die upon the sinking of a ship. This is the code I am supposed to manipulate:

##EDIT VARIABLES TO THE VARIABLES OF INTEREST
train_var = train.loc[:,['Survived','Sex']]  
test_var = test.loc[:,['Sex']]  
BayesNet = BayesianModel([('Sex','Survived')])

I am supposed to add another variable, 'Pclass,' to the mix, paying attention to the order for causation. I have added that variable to every line of this code in every way imaginable and consistently get an error from this line:

predictions = pandas.DataFrame({'PassengerId': test.PassengerId,'Survived': hypothesis.Survived.tolist()})
predictions

For example, the error I get for this version of the code:

train_var = train.loc[:,['Survived','Pclass','Sex']]  
test_var = test.loc[:,['Pclass']]  
BayesNet = BayesianModel([('Sex','Pclass','Survived')])

is this:

AttributeError                            Traceback (most recent call last)
<ipython-input-98-16d9eb9451f7> in <module>
----> 1 predictions = pandas.DataFrame({'PassengerId': test.PassengerId,'Survived': hypothesis.Survived.tolist()})
      2 predictions

/opt/conda/lib/python3.7/site-packages/pandas/core/generic.py in __getattr__(self, name)
   5137             if self._info_axis._can_hold_identifiers_and_holds_name(name):
   5138                 return self[name]
-> 5139             return object.__getattribute__(self, name)
   5140 
   5141     def __setattr__(self, name: str, value) -> None:

AttributeError: 'DataFrame' object has no attribute 'Survived'

Honestly, I have no idea wtf any of this means. I have tried googling this issue and have come up with nothing.

Any help would be greatly appreciated. I know it's a lot.

🌐
GitHub
github.com › pandas-dev › pandas › issues › 11946
Series doesn't have an is_unique attribute · Issue #11946 · pandas-dev/pandas
January 3, 2016 - Hello, Series doesn't have is a is_unique attribute Index have a is_unique attribute but not (values of a) Series >>> df.index.is_unique True >>> df['Column1'].is_unique -------------------------------------------------------------------...
Author   s-celles
🌐
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...
Top answer
1 of 5
2

"sklearn.datasets" is a scikit package, where it contains a method load_iris().

load_iris(), by default return an object which holds data, target and other members in it. In order to get actual values you have to read the data and target content itself.

Whereas 'iris.csv', holds feature and target together.

FYI: If you set return_X_y as True in load_iris(), then you will directly get features and target.

from sklearn import datasets
data,target = datasets.load_iris(return_X_y=True)
2 of 5
1

The Iris Dataset from Sklearn is in Sklearn's Bunch format:

print(type(iris))
print(iris.keys())

output:

<class 'sklearn.utils.Bunch'>
dict_keys(['data', 'target', 'target_names', 'DESCR', 'feature_names', 'filename'])

So, that's why you can access it as:

x=iris.data
y=iris.target

But when you read the CSV file as DataFrame as mentioned by you:

iris = pd.read_csv('iris.csv',header=None).iloc[:,2:4]
iris.head()

output is:

    2   3
0   petal_length    petal_width
1   1.4 0.2
2   1.4 0.2
3   1.3 0.2
4   1.5 0.2

Here the column names are '1' and '2'.

First of all you should read the CSV file as:

df = pd.read_csv('iris.csv')

you should not include header=None as your csv file includes the column names i.e. the headers.

So, now what you can do is something like this:

X = df.iloc[:, [2, 3]] # Will give you columns 2 and 3 i.e 'petal_length' and 'petal_width'
y = df.iloc[:, 4] # Label column i.e 'species'

or if you want to use the column names then:

X = df[['petal_length', 'petal_width']]
y = df.iloc['species']

Also, if you want to convert labels from string to numerical format use sklearn LabelEncoder

from sklearn import preprocessing
le = preprocessing.LabelEncoder()
y = le.fit_transform(y)
🌐
Edureka Community
edureka.co › home › community › categories › questions › python pandas error attributeerror dataframe ...
Python Pandas error AttributeError DataFrame object has no attribute make | Edureka Community
May 1, 2020 - i was trying to print unique values in my data %matplotlib inline import pandas as pd import ... : 'DataFrame' object has no attribute 'Make'
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.unique.html
pandas.Series.unique — pandas 3.0.2 documentation - PyData |
Return unique values of Series object. Uniques are returned in order of appearance. Hash table-based unique, therefore does NOT sort.