In this Python tutorial, we will learn various methods to get unique values in Pandas DataFrame in Python. We’ll use some built-in functions to understand different approaches to getting unique values in Pandas DataFrame.
As a Developer, while making the Python Project I got the requirement to get unique values from a dataframe in Python.
Here we will see:
- How to get unique values in Pandas DataFrame using pandas.unique().
- How to get unique values in Pandas DataFrame using drop_duplicate()
- How To extract unique values from the Pandas column using unique()
How to get unique values in Pandas DataFrame
In Python, there are primarily three methods that are commonly used and important to understand to get unique values in Python Pandas DataFrame.
How to get unique values in Pandas DataFrame using pandas.unique()
- In this section, we will discuss how to get unique values in Python Pandas DataFrame using pandas.unique().
- When there are several similar values in a column, Unique eliminates all duplicate values and delivers a single value.
- When working with a single column of a DataFrame, the unique() method is utilized and returns every unique element in the column. A DataFrame containing the different components of a column and their corresponding index labels is the result of the method.
Syntax:
Here is the Syntax of pandas.unique() in Python Pandas.
pandas.unique(values)
Note: When working with 1-Dimensional data, the mentioned syntax is helpful. From the 1-Dimensional data values, it represents the singular value (Series data structure).
Example:
Let’s take an example and check how to get unique values in Pandas DataFrame using pandas.unique().
Here we will take one-dimensional elements to get the unique values from the Python list
Source Code:
import pandas as pd
# Input list
new_list=[12,34,45,67,67,34]
# Using dataframe.unique() function
result= pd.unique(new_list)
# Display the Content
print("Get all unique values:", result)
In the following given code first, we created the input list and now we want to extract the unique values from the input list using the pd.unique() function within this function, we assigned the one-dimensional list as an argument.
Here is the implementation of the following given code.
This is how to get unique values in Pandas DataFrame using pandas.unique().
Read: Get index Pandas Python
How to get unique values in Pandas DataFrame using drop_duplicate()
- Now let us understand how to get unique values in Pandas DataFrame using drop_duplicate().
- Duplicate rows in a DataFrame can be removed using the pandas.DataFrame.drop_duplicate() method. You can drop duplicate rows on specific multiple columns or all columns using this technique.
- The drop_duplicate() method is considered to be the faster way to eliminate duplicate values when working with a large set of dataframes.
Syntax:
Here is the Syntax of drop_duplicate() in Python Pandas DataFrame.
DataFrame.drop_duplicates(subset=None, *, keep='first', inplace=False, ignore_index=False)
- It consists of a few parameters
- subset: Use only specific columns rather than all of them by default when looking for duplicates.
- keep: By default, it takes a first value.
- inplace: if the DataFrame should be changed rather than a new one.
- ignore_index: If True, the resulting axis will have the numbers 0, 1, and so forth.
Example:
Let’s take an example and check how to get unique values in Pandas DataFrame using drop_duplicate().
Source Code:
import pandas as pd
employee_info = {
"emp_name": ["George", "Micheal", "John"],
"emp_id": [22,22,45]
}
# By Using DataFrame()
df = pd.DataFrame(employee_info)
print(df.drop_duplicates(subset = "emp_id"))
In the above code first, we imported the Pandas library and then created a dictionary as employee information in which we created two columns ’emp_name’ and ’emp_id’.
Next, we will convert the input dictionary into a dataframe using the pd.dataframe() function and within this function, we passed the dictionary as a parameter.
Now we used the df.drop,duplicates() method for extracting the unique values from the dataframe.
Here is the implementation of the following given code.
As you can see in the Screenshot, we have discussed how to get unique values in Pandas DataFrame using drop_duplicate().
Read: Python Pandas CSV Tutorial
How To extract unique values from the Pandas column using unique()
- In this section, we will discuss how to extract unique values from the Pandas column using unique().
- When working with the DataFrame in Pandas, you should identify the unique items in the column. To execute this, we must extract the unique values from the columns using the unique() method. We can quickly find unique data with the Python Pandas package.
Syntax:
Let’s have a look at the Syntax and understand the working pandas.Series.unique() in Python.
Series.unique()
Note: Using the Series, the unique() method returns a NumPy ndarray of unique values.
Example:
In this example, we will extract the unique values from the dataframe’s Country_name column. Therefore, the output will contain only unique values from the Country_name column.
Source Code:
# import pandas
import pandas as pd
# Creating DataFrame
df = pd.DataFrame({'Country_name':['U.S.A','U.S.A','Germany','China','China'],'Zip_code':[86734,86734,94562,74563,74563]})
# Extract the unique values
print(df['Country_name'].unique())
In the above code first, we created a dataframe in which we assigned two column names ‘Country_name’ and’Zip_Code’. Now we want to get the unique values from the ‘Country_name’ column. For this, we used the series.unique() function.
Here is the implementation of the following given code
This is how to extract unique values from the Pandas column using unique.
You may also like to read the following Python Pandas tutorials.
- How to convert floats to integer in Pandas
- How to convert floats to integer in Pandas
- Missing Data in Pandas in Python
- Python Pandas DataFrame Iterrows
- How to drop rows with NaN or missing values in Pandas DataFrame
In this article, we have discussed various methods to get unique values in Pandas DataFrame in Python. And also we have covered the following given topics.
- How to get unique values in Pandas DataFrame using pandas.unique().
- How to get unique values in Pandas DataFrame using drop_duplicate()
- How To extract unique values from the Pandas column using unique()
Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.