Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas `transform(set)` raising exception

I'm trying to transform by set, but I'm getting an exception. Transform works fine with "sum" and many other aggregate functions, but not with set or list.

>>> import pandas as pd
>>> df = pd.DataFrame({"a":[1,2,1,], "b":[1,1,2]})
>>> df
   a  b
0  1  1
1  2  1
2  1  2
>>> df.groupby("a").b.transform(set)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/avloss/conda/lib/python3.7/site-packages/pandas/core/groupby/generic.py", line 1032, in transform
    s = klass(res, indexer)
  File "/Users/avloss/conda/lib/python3.7/site-packages/pandas/core/series.py", line 282, in __init__
    "{0!r} type is unordered" "".format(data.__class__.__name__)
TypeError: 'set' type is unordered

What I was expecting was:

   a      b   
0  1  {1, 2}
1  2     {1}
2  1  {1, 2}
like image 243
avloss Avatar asked Oct 19 '25 01:10

avloss


1 Answers

Easiest way I can think of is aggregate as set and map it back

df['new_col'] = df['a'].map(df.groupby('a')['b'].agg(set))
print(df)

   a  b new_col
0  1  1  {1, 2}
1  2  1     {1}
2  1  2  {1, 2}
like image 83
anky Avatar answered Oct 21 '25 16:10

anky