To find unique values from a single column, use the unique() method. Let’s say you have Employee Records in your Pandas DataFrame, so the names can get repeated since two employees can have similar names. 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 from the DataFrame column “EmpName” −
dataFrame['EmpName'].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 value from a single column print(f"\nUnique Name of Employees = {dataFrame['EmpName'].unique()}")
Output
This will produce the following output −
DataFrame1 ... EmpName Zone 0 John North 1 Ted South 2 Jacob South 3 Scarlett East 4 Ami West 5 Ted East 6 Scarlett North Unique Name of Employees = ['John' 'Ted' 'Jacob' 'Scarlett' 'Ami']