如何为 size()列分配名称?

我使用 .size()的分组结果,以计算有多少项目在每个组。

我希望结果被保存到一个新的列名,而无需手动编辑列名数组,如何做到这一点?

这就是我所尝试的:

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

我得到的错误是:

TypeError: “ DataFrameGroupBy”对象不支持项分配 (在第二行)

81943 次浏览

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.)

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

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.

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

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