Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to assign a name to the size() column?

Tags:

python

pandas

I am using .size() on a groupby result in order to count how many items are in each group.

I would like the result to be saved to a new column name without manually editing the column names array, how can it be done?

This is what I have tried:

grpd = df.groupby(['A','B']) grpd['size'] = grpd.size() grpd 

and the error I got:

TypeError: 'DataFrameGroupBy' object does not support item assignment (on the second line)

like image 377
d1337 Avatar asked Aug 01 '13 13:08

d1337


People also ask

How do I change the column name after Groupby?

The current (as of version 0.20) method for changing column names after a groupby operation is to chain the rename method. See this deprecation note in the documentation for more detail.


1 Answers

The .size() built-in method of DataFrameGroupBy objects actually returns a Series object with the group sizes and not a DataFrame. If you want a DataFrame whose column is the group sizes, indexed by the groups, with a custom name, you can use the .to_frame() method and use the desired column name as its argument.

grpd = df.groupby(['A','B']).size().to_frame('size') 

If you wanted the groups to be columns again you could add a .reset_index() at the end.

like image 102
Sealander Avatar answered Sep 17 '22 04:09

Sealander