Python 如何为 a size() 列指定名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17995024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:41:19  来源:igfitidea点击:

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

pythonpandas

提问by d1337

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

我在 groupby 结果上使用 .size() 以计算每个组中有多少项目。

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?

我想在不手动编辑列名数组的情况下将结果保存到新的列名,怎么办?

Thanks

谢谢

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)

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

采纳答案by Dan Allan

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.

的结果df.groupby(...)不是 DataFrame。要取回 DataFrame,您必须对每个组应用一个函数、转换组的每个元素或过滤组。

It seems like you want a DataFrame that contains (1) all your original data in dfand (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.

似乎您想要一个包含 (1) 所有原始数据df和 (2) 每组中有多少数据的数据帧。这些东西有不同的长度,所以如果它们需要进入同一个DataFrame,你需要冗余地列出大小,即每组中的每一行。

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

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

(旁白:如果您能显示简洁的样本输入和预期结果,这会很有帮助。)

回答by jezrael

You need transformsize- lenof dfis same as before:

你需要- of和以前一样:transformsizelendf

Notice:

注意:

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

这里有必要在 之后添加一列groupby,否则会出错。因为GroupBy.sizecountNaN也一样,使用什么列并不重要。所有列的工作方式相同。

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- lenof dfis obviously NOTsame as before:

如果需要在聚集集列名df-lendf显然不是像以前一样:

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

回答by Sealander

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.

.size()DataFrameGroupBy 对象的内置方法实际上返回一个带有组大小的 Series 对象,而不是一个 DataFrame。如果您想要一个 DataFrame,其列是组大小,由组索引,具有自定义名称,您可以使用该.to_frame()方法并使用所需的列名作为其参数。

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.

如果您希望组再次成为列,您可以.reset_index()在末尾添加一个。

回答by Sealander

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

假设 n 是数据框的名称, cst 是重复的项目数。下面的代码给出了下一列的计数

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

希望这会奏效