
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Pandas - Get unique values from a column
To get unique values from a column in a DataFrame, use the unique(). To count the unique values from a column in a DataFrame, use the nunique().
At first, import the required library −
import pandas as pd;
Create a DataFrame with 3 columns. We have duplicate values as well −
dataFrame = pd.DataFrame( { "Car": ['BMW', 'Audi', 'BMW', 'Lexus', 'Tesla', 'Lexus', 'Mustang'],"Place": ['Delhi','Bangalore','Hyderabad','Chandigarh','Pune', 'Mumbai', 'Jaipur'],"Units": [100, 150, 50, 110, 90, 120, 80] } )
Get unique values with the count −
print"\nUnique values from a column ...\n",dataFrame['Car'].unique() print"\nCount unique values from a column ...\n",dataFrame['Car'].nunique()
Example
Following is the code −
import pandas as pd; # create a DataFrame dataFrame = pd.DataFrame( { "Car": ['BMW', 'Audi', 'BMW', 'Lexus', 'Tesla', 'Lexus', 'Mustang'],"Place": ['Delhi','Bangalore','Hyderabad','Chandigarh','Pune', 'Mumbai', 'Jaipur'],"Units": [100, 150, 50, 110, 90, 120, 80] } ) print"DataFrame ...\n",dataFrame # get unique values from a column print"\nUnique values from a column ...\n",dataFrame['Car'].unique() print"\nCount unique values from a column ...\n",dataFrame['Car'].nunique()
Output
This will produce the following output −
DataFrame ... Car Place Units 0 BMW Delhi 100 1 Audi Bangalore 150 2 BMW Hyderabad 50 3 Lexus Chandigarh 110 4 Tesla Pune 90 5 Lexus Mumbai 120 6 Mustang Jaipur 80 Unique values from a column ... ['BMW' 'Audi' 'Lexus' 'Tesla' 'Mustang'] Count unique values from a column ... 5
- Related Articles
- Python Pandas – Find unique values from a single column
- Python Pandas - Display unique values present in each column
- Get unique values from a list in Python
- Python Pandas – Find unique values from multiple columns
- Python Pandas - Return a Series containing counts of unique values from Index object
- How to get column index from column name in Python Pandas?
- Count unique values per groups in Python Pandas
- Python Pandas - Return unique values in the index
- Python Pandas - Return a Series containing counts of unique values from Index object considering NaN values as well
- Python Pandas - Check if the index has unique values
- How to get unique values from MongoDB collection?
- Get distinct values from a column in MongoDB?
- Python Program to print unique values from a list
- Select rows from a Pandas DataFrame based on column values
- Python Pandas - Return a Series containing counts of unique values from Index object sorted in Ascending Order

Advertisements