I am using pandas groupby and want to use this function to create a set of elements in a group.
The following does not work:
df = df.groupby('col1')['col2'].agg({'size': len, 'set': set})
But the following works:
def to_set(x):
return set(x)
df = df.groupby('col1')['col2'].agg({'size': len, 'set': to_set})
In my understanding, the two expressions are similar, what is the reason why the first does not work?
source
share