Pandas groupby and make set of items

前端 未结 2 2056
借酒劲吻你
借酒劲吻你 2020-12-10 13:19

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

相关标签:
2条回答
  • 2020-12-10 14:14

    Try using:

    df = df.groupby('col1')['col2'].agg({'size': len, 'set': lambda x: set(x)})
    

    Works for me.

    0 讨论(0)
  • 2020-12-10 14:21

    Update

    • As late as pandas version 0.22, this is an issue.
    • As of pandas version 1.1.2, this is not an issue. Aggregating set, doesn't result in TypeError: 'type' object is not iterable.
      • Not certain when the functionality was updated.

    Original Answer

    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 : function or dict

    Function to use for aggregating groups.

    • If a function, must either work when passed a DataFrame or when passed to DataFrame.apply.
    • If passed a dict, the keys must be DataFrame column names.

    Accepted Combinations are:

    • string cythonized function name
    • function
    • list of functions
    • dict of columns -> functions
    • nested dict of names -> dicts of functions
    0 讨论(0)
提交回复
热议问题