Python - Typecasting Pandas into set


To typecast pandas into Set, use the set(). At first, let us create a DataFrame −

dataFrame = pd.DataFrame(
   {
      "EmpName": ['John', 'Ted', 'Jacob', 'Scarlett', 'Ami', 'Ted', 'Scarlett'],
      "Zone": ['North', 'South', 'South', 'East', 'West', 'East', 'North']
   }
)

Typecast pandas to set and then take set union −

set(dataFrame.EmpName) | set(dataFrame.Zone)

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

# Pandas into Set
print"\nTypecast Pandas into Set...\n",set(dataFrame.EmpName) | set(dataFrame.Zone)

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

Typecast Pandas into Set...
set(['Ami', 'East', 'North', 'West', 'Ted', 'South', 'Jacob', 'John', 'Scarlett'])

Updated on: 15-Sep-2021

97 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements