
- 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 – Find unique values from multiple columns
To find unique values from multiple columns, use the unique() method. Let’s say you have Employee Records with “EmpName” and “Zone” in your Pandas DataFrame. The name and zone can get repeated since two employees can have similar names and a zone can have more than one employee. In that case, if you want unique Employee names, then use the unique() for DataFrame.
At first, import the required library. Here, we have set pd as an alias −
import pandas as pd
At first, create a DataFrame. Here, we have two columns −
dataFrame = pd.DataFrame( { "EmpName": ['John', 'Ted', 'Jacob', 'Scarlett', 'Ami', 'Ted', 'Scarlett'],"Zone": ['North', 'South', 'South', 'East', 'West', 'East', 'North'] } )
Fetch unique Employee Names and Zone from the DataFrame column “EmpName” and “Zone” −
{pd.concat([dataFrame['EmpName'],dataFrame['Zone']]).unique()}
Example
Following is the complete code −
import pandas as pd # Create DataFrame dataFrame = pd.DataFrame( { "EmpName": ['John', 'Ted', 'Jacob', 'Scarlett', 'Ami', 'Ted', 'Scarlett'],"Zone": ['North', 'South', 'South', 'East', 'West', 'East', 'North'] } ) print("DataFrame ...\n",dataFrame) # Fetch unique values from multiple columns print(f"\nFetching unique Values from the two columns and concatenate them:\n \ {pd.concat([dataFrame['EmpName'],dataFrame['Zone']]).unique()}")
Output
This will produce the following output −
DataFrame ... EmpName Zone 0 John North 1 Ted South 2 Jacob South 3 Scarlett East 4 Ami West 5 Ted East 6 Scarlett North Fetching unique Values from the two columns and concatenate them: ['John' 'Ted' 'Jacob' 'Scarlett' 'Ami' 'North' 'South' 'East' 'West']
- Related Articles
- Python Pandas – Find unique values from a single column
- Python - Select multiple columns from a Pandas dataframe
- Python Pandas - Get unique values from a column
- 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
- Python Pandas - Plot multiple data columns in a DataFrame?
- Create a Pivot Table with multiple columns – Python Pandas
- Python Pandas - Display unique values present in each column
- Python Pandas - Check if the index has unique values
- Python Pandas - Fill missing columns values (NaN) with constant values
- Python Pandas - Return a Series containing counts of unique values from Index object considering NaN values as well
- How to find the number of unique values of multiple categorical columns based on one categorical column in R?
- Python Pandas - Create a subset by choosing specific values from columns based on indexes
- Select multiple columns in a Pandas DataFrame

Advertisements