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']