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

前端 未结 1 1605
南旧
南旧 2021-01-30 11:04

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

1条回答
  •  迷失自我
    2021-01-30 11:32

    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
    

    0 讨论(0)
提交回复
热议问题