Python pandas:向我的数据框中添加一列计算变量的列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29791785/
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 05:01:16  来源:igfitidea点击:

Python pandas: Add a column to my dataframe that counts a variable

pythonpandascountgroup-by

提问by UserYmY

I have a dataframe 'gt' like this:

我有一个像这样的数据框“gt”:

org     group
org1      1
org2      1
org3      2
org4      3
org5      3
org6      3

and I would like to add column 'count' to gt dataframe to counts number member of the groups, expected results like this:

我想将列“计数”添加到 gt 数据框以计算组的成员数量,预期结果如下:

org     group   count
org1      1       2
org2      1       2
org3      2       1
org4      3       3
org5      3       3
org6      3       3

I know how to do it per one item of the group, but do not know how to make the count repeated for all of the group items, here is the code I have used:

我知道如何为组中的每个项目执行此操作,但不知道如何为所有组项目重复计数,这是我使用的代码:

gtcounts = gt.groupby('group').count()

Can anybody help?

有人可以帮忙吗?

采纳答案by EdChum

Call transformthis will return a Series aligned with the original df:

调用transform这将返回一个与原始 df 对齐的系列:

In [223]:

df['count'] = df.groupby('group')['group'].transform('count')
df
Out[223]:
    org  group  count
0  org1      1      2
1  org2      1      2
2  org3      2      1
3  org4      3      3
4  org5      3      3
5  org6      3      3