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

僤鯓⒐⒋嵵緔 提交于 2019-12-20 08:40:36

问题


I have a dataframe 'gt' like this:

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:

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?


回答1:


Call transform this will return a Series aligned with the original 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


来源:https://stackoverflow.com/questions/29791785/python-pandas-add-a-column-to-my-dataframe-that-counts-a-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!