Abhishek Niranjan
Abhishek Niranjan

Reputation: 153

How to take set union of all the values in a column of pandas Dataframe?

First two rows of a dataframe, df:

0|50331648|{1,2,3,4,5}|6  
1|50331649|{3,5,7,8}|2  

After performing the operation, I just need a set that contains {1,2,3,4,5,7,8}.

How to achieve it?

Upvotes: 6

Views: 4756

Answers (1)

Nickil Maveli
Nickil Maveli

Reputation: 29719

Assuming "B" to be the column name under consideration, you could use set.union on the obtained unpacked list:

set.union(*df['B'].tolist())
{1, 2, 3, 4, 5, 7, 8}

(Or)

Supply these as a callable function to reduce:

from functools import reduce      # If you're on Py3k
reduce(set.union, df['B'].tolist())
{1, 2, 3, 4, 5, 7, 8}

Data:

df = pd.DataFrame(dict(A=[50331648, 50331649],
                       B=[{1,2,3,4,5}, {3,5,7,8}],
                       C=[6,2])
                 )

enter image description here

Upvotes: 6

Related Questions