I am using pandas groupby and want to apply the function to make a set from the items in the group.
The following results in TypeError: 'type' object is not iterable
:
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 expression are similar, what is the reason why the first does not work?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…