Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intersection of sets as columns in pandas

I have a df such as:

df=pd.DataFrame.from_items([('i', [set([1,2,3,4]), set([1,2,3,4]), set([1,2,3,4]),set([1,2,3,4])]), ('j', [set([2,3]), set([1]), set([4]),set([3,4])])])

so it looks like

>>> df
              i       j
0  {1, 2, 3, 4}  {2, 3}
1  {1, 2, 3, 4}     {1}
2  {1, 2, 3, 4}     {4}
3  {1, 2, 3, 4}  {3, 4}

I would like to compute df.i.intersection(df.j) and assign that to be column k. That is, I want this:

df['k']=[df.i.iloc[t].intersection(df.j.iloc[t]) for t in range(4)]

>>> df.k
0    {2, 3}
1       {1}
2       {4}
3    {3, 4}
Name: k, dtype: object

Is there a df.apply() for this? The actual df is millions of rows.

like image 607
Kevin Avatar asked Jul 21 '17 13:07

Kevin


1 Answers

Working with sets, lists and dicts in pandas is a bit problematic, because best working with scalars:

df['k'] = [x[0] & x[1] for x in zip(df['i'], df['j'])]
print (df)
              i       j       k
0  {1, 2, 3, 4}  {2, 3}  {2, 3}
1  {1, 2, 3, 4}     {1}     {1}
2  {1, 2, 3, 4}     {4}     {4}
3  {1, 2, 3, 4}  {3, 4}  {3, 4}

df['k'] = [x[0].intersection(x[1]) for x in zip(df['i'], df['j'])]
print (df)
              i       j       k
0  {1, 2, 3, 4}  {2, 3}  {2, 3}
1  {1, 2, 3, 4}     {1}     {1}
2  {1, 2, 3, 4}     {4}     {4}
3  {1, 2, 3, 4}  {3, 4}  {3, 4}

Solution with apply:

df['k'] = df.apply(lambda x: x['i'].intersection(x['j']), axis=1)
print (df)
              i       j       k
0  {1, 2, 3, 4}  {2, 3}  {2, 3}
1  {1, 2, 3, 4}     {1}     {1}
2  {1, 2, 3, 4}     {4}     {4}
3  {1, 2, 3, 4}  {3, 4}  {3, 4}
like image 197
jezrael Avatar answered Oct 06 '22 23:10

jezrael