Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas groupby and make set of items

Tags:

python

pandas

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?

like image 270
hangc Avatar asked Jun 01 '16 15:06

hangc


People also ask

How do you group rows together in a DataFrame?

You can group DataFrame rows into a list by using pandas. DataFrame. groupby() function on the column of interest, select the column you want as a list from group and then use Series. apply(list) to get the list for every group.

How do I group values in a column in pandas?

Groupby is a very powerful pandas method. You can group by one column and count the values of another column per this column value using value_counts. Using groupby and value_counts we can count the number of activities each person did.


3 Answers

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
like image 196
Stefan Avatar answered Oct 24 '22 03:10

Stefan


Try using:

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

Works for me.

like image 36
Animesh Mishra Avatar answered Oct 24 '22 03:10

Animesh Mishra


Update for newer versions of Pandas if you get the following error

SpecificationError: nested renamer is not supported
df = df.groupby('col1')['col2'].agg(size= len, set= lambda x: set(x))
like image 4
kindjacket Avatar answered Oct 24 '22 03:10

kindjacket