AttributeError: ‘dataframe’ object has no attribute ‘unique’ ( Solved )

AttributeError_ 'dataframe' object has no attribute 'unique' ( Solved )

If you want to manipulate any datasets then you have to first convert them to dataframe and after that use a specific function 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’. In this tutorial, you will learn how to solve this error with a simple solution.

Why ‘dataframe’ object has no attribute ‘unique’ Error comes?

The main reason why you are getting this error is that you must not be properly using the unique() attribute. Most of the cases the error will come when you will use the unique() function on the entire dataframe.

You will get the ‘dataframe’ object has no attribute ‘unique’ error when you will run the below lines of code.

import pandas as pd
import numpy as np
data = {"name":["Rob","Bam","Maya","Rob"],"age":[23,25,26,23],
        "country":["USA","UK","France","USA"]}
df = pd.DataFrame(data)
df.unique()

Output

'DataFrame' object has no attribute 'unique'
‘DataFrame’ object has no attribute ‘unique’

The solution of ‘dataframe’ object has no attribute ‘unique’

The solution to this AttributeError is that you have to use a unique() attribute on a specific column, not the entire dataframe. For example, if I want to find the unique on the name column name then I will use the below lines of code.

import pandas as pd
import numpy as np
data = {"name":["Rob","Bam","Maya","Rob"],"age":[23,25,26,23],
        "country":["USA","UK","France","USA"]}
df = pd.DataFrame(data)
df["name"].unique()

Output

Unique value for the name column
Unique value for the name column

Here you can see I am getting the unique values for the “name” column of the object type. You can get the list of unique values using the df[“name”].unique()[0]

Conclusion

Most of the attributes of the dataframe are applied on specific columns, not on the full dataframe. So always check that are you using functions or attributes on the full dataframe or on a specific column or not. If you are getting the above problem then the above solution will solve your error.

I hope you have liked this tutorial. If you have any queries then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner