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 i
Try using:
df = df.groupby('col1')['col2'].agg({'size': len, 'set': lambda x: set(x)})
Works for me.
set, doesn't result in TypeError: 'type' object is not iterable.
It's because set is of type type whereas to_set is of type function:
type(set)
<class 'type'>
def to_set(x):
return set(x)
type(to_set)
<class 'function'>
According to the docs, .agg() expects:
arg :
functionordict
Function to use for aggregating groups.
- If a
function, must either work when passed aDataFrameor when passed toDataFrame.apply.
- If passed a
dict, the keys must beDataFramecolumn names.
Accepted Combinations are:
stringcythonized function namefunction
listof functions
dictof columns -> functions
- nested
dictof names -> dicts of functions