Python - Typecasting Pandas into set

PythonServer Side ProgrammingProgramming

<p>To typecast pandas into Set, use the set(). At first, let us create a DataFrame &minus;</p><pre class="just-code notranslate language-python" data-lang="python">dataFrame = pd.DataFrame( { &quot;EmpName&quot;: [&#39;John&#39;, &#39;Ted&#39;, &#39;Jacob&#39;, &#39;Scarlett&#39;, &#39;Ami&#39;, &#39;Ted&#39;, &#39;Scarlett&#39;], &quot;Zone&quot;: [&#39;North&#39;, &#39;South&#39;, &#39;South&#39;, &#39;East&#39;, &#39;West&#39;, &#39;East&#39;, &#39;North&#39;] } ) </pre><p>Typecast pandas to set and then take set union &minus;</p><pre class="just-code notranslate language-python" data-lang="python">set(dataFrame.EmpName) | set(dataFrame.Zone)</pre><h2>Example</h2><p>Following is the complete code &minus;</p><pre class="demo-code notranslate language-python" data-lang="python"> import pandas as pd # Create DataFrame dataFrame = pd.DataFrame( { &quot;EmpName&quot;: [&#39;John&#39;, &#39;Ted&#39;, &#39;Jacob&#39;, &#39;Scarlett&#39;, &#39;Ami&#39;, &#39;Ted&#39;, &#39;Scarlett&#39;], &quot;Zone&quot;: [&#39;North&#39;, &#39;South&#39;, &#39;South&#39;, &#39;East&#39;, &#39;West&#39;, &#39;East&#39;, &#39;North&#39;] } ) print&quot;DataFrame ... &quot;,dataFrame # Pandas into Set print&quot; Typecast Pandas into Set... &quot;,set(dataFrame.EmpName) | set(dataFrame.Zone) </pre><h2>Output</h2><p>This will produce the following output &minus;</p><pre class="result notranslate">DataFrame ... &nbsp; &nbsp; EmpName Zone 0 &nbsp; &nbsp; &nbsp;John North 1 &nbsp; &nbsp; &nbsp; Ted South 2 &nbsp; &nbsp; Jacob South 3 &nbsp;Scarlett East 4 &nbsp; &nbsp; &nbsp; Ami West 5 &nbsp; &nbsp; &nbsp; Ted East 6 &nbsp;Scarlett North Typecast Pandas into Set... set([&#39;Ami&#39;, &#39;East&#39;, &#39;North&#39;, &#39;West&#39;, &#39;Ted&#39;, &#39;South&#39;, &#39;Jacob&#39;, &#39;John&#39;, &#39;Scarlett&#39;])</pre>
raja
Updated on 15-Sep-2021 07:58:46

Advertisements