In the article, we will see what causes the “dataframe object has no attribute ‘unique’ error“.
The main reason why this error comes is when we use the unique()
method on the data frames. We can use the unique()
method on a Series object to get an array of unique values. But, this method is not available for DataFrames.
When using the DataFrames use any of the following methods to get the unique values from the DataFrames
drop_duplicates() method
Using the drop duplicates method we can remove the duplicate rows from a DataFrame.
- The
drop_duplicates()
method can be used to remove duplicate rows from a DataFrame. The subset parameter specifies which columns to consider when looking for duplicates. - If we want to get a list of unique values from a specific column in the DataFrame, we can use the
drop_duplicates()
method on just that column.
Following is the example:
import pandas as pd
df = pd.DataFrame({'col1': ['a', 'a', 'b', 'c'], 'col2': [1, 2, 2, 3]})
# Get unique values from a specific column
unique_col1 = df['col1'].drop_duplicates().tolist()
# Get unique rows based on multiple columns
unique_rows = df.drop_duplicates(subset=['col1', 'col2'])
numpy.unique() method
Using the numpy.unique() method, we can get the unique values from the multiple columns in a DataFrame. This method returns the sorted unique elements of an array.
import pandas as pd
import numpy as np
df = pd.DataFrame({'col1': ['a', 'a', 'b', 'c'], 'col2': [1, 2, 2, 3]})
# Get unique values from multiple columns
unique_values = np.unique(df[['col1', 'col2']])
set() method
If you want to get unique values from a single column in a DataFrame, you can convert the column to a set using the set() function. This will remove all the duplicates and returns the set with unique values.
import pandas as pd
df = pd.DataFrame({'col1': ['a', 'a', 'b', 'c'], 'col2': [1, 2, 2, 3]})
# Get unique values from a single column
unique_col1 = set(df['col1'])