如何为 size() 列指定名称? [英] How to assign a name to the size() column?

查看:34
本文介绍了如何为 size() 列指定名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

这是我尝试过的:

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

以及我得到的错误:

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

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

推荐答案

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

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.

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

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

这篇关于如何为 size() 列指定名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆