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

PythonPandas

Python Problem Overview


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)

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

You need transform size - len of df is same as before:

Notice:

Here it is necessary to add one column after groupby, else you get an error. Because GroupBy.size count NaNs too, what column is used is not important. All columns working same.

import pandas as pd

df = pd.DataFrame({'A': ['x', 'x', 'x','y','y']
                , 'B': ['a', 'c', 'c','b','b']})
print (df)
   A  B
0  x  a
1  x  c
2  x  c
3  y  b
4  y  b

df['size'] = df.groupby(['A', 'B'])['A'].transform('size')
print (df)
   A  B  size
0  x  a     1
1  x  c     2
2  x  c     2
3  y  b     2
4  y  b     2

If need set column name in aggregating df - len of df is obviously NOT same as before:

import pandas as pd

df = pd.DataFrame({'A': ['x', 'x', 'x','y','y']
                , 'B': ['a', 'c', 'c','b','b']})
print (df)
   A  B
0  x  a
1  x  c
2  x  c
3  y  b
4  y  b

df = df.groupby(['A', 'B']).size().reset_index(name='Size')
print (df)
   A  B  Size
0  x  a     1
1  x  c     2
2  y  b     2

Solution 3 - Python

The result of df.groupby(...) is not a DataFrame. To get a DataFrame back, you have to apply a function to each group, transform each element of a group, or filter the groups.

It seems like you want a DataFrame that contains (1) all your original data in df and (2) the count of how much data is in each group. These things have different lengths, so if they need to go into the same DataFrame, you'll need to list the size redundantly, i.e., for each row in each group.

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

(Aside: It's helpful if you can show succinct sample input and expected results.)

Solution 4 - Python

You can set the as_index parameter in groupby to False to get a DataFrame instead of a Series:

df = pd.DataFrame({'A': ['a', 'a', 'b', 'b'], 'B': [1, 2, 2, 2]})

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

Output:

   A  B  size
0  a  1     1
1  a  2     1
2  b  2     2

Solution 5 - Python

lets say n is the name of dataframe and cst is the no of items being repeted. Below code gives the count in next column

cstn=Counter(n.cst)
cstlist = pd.DataFrame.from_dict(cstn, orient='index').reset_index()
cstlist.columns=['name','cnt']
n['cnt']=n['cst'].map(cstlist.loc[:, ['name','cnt']].set_index('name').iloc[:,0].to_dict())

Hope this will work

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiond1337View Question on Stackoverflow
Solution 1 - PythonSealanderView Answer on Stackoverflow
Solution 2 - PythonjezraelView Answer on Stackoverflow
Solution 3 - PythonDan AllanView Answer on Stackoverflow
Solution 4 - PythonMykola ZotkoView Answer on Stackoverflow
Solution 5 - Pythonuser8403237View Answer on Stackoverflow