pandas add column to groupby dataframe

前端 未结 2 1403
孤城傲影
孤城傲影 2020-11-22 15:29

I have this simple dataframe df:

df = pd.DataFrame({\'c\':[1,1,1,2,2,2,2],\'type\':[\'m\',\'n\',\'o\',\'m\',\'m\',\'n\',\'n\']})
相关标签:
2条回答
  • 2020-11-22 16:01

    Use transform to add a column back to the orig df from a groupby aggregation, transform returns a Series with its index aligned to the orig df:

    In [123]:
    g = df.groupby('c')['type'].value_counts().reset_index(name='t')
    g['size'] = df.groupby('c')['type'].transform('size')
    g
    
    Out[123]:
       c type  t  size
    0  1    m  1     3
    1  1    n  1     3
    2  1    o  1     3
    3  2    m  2     4
    4  2    n  2     4
    
    0 讨论(0)
  • 2020-11-22 16:20

    Another solution with transform len:

    df['size'] = df.groupby('c')['type'].transform(len)
    print df
       c type size
    0  1    m    3
    1  1    n    3
    2  1    o    3
    3  2    m    4
    4  2    m    4
    5  2    n    4
    6  2    n    4
    

    Another solution with Series.map and Series.value_counts:

    df['size'] = df['c'].map(df['c'].value_counts())
    print (df)
       c type  size
    0  1    m     3
    1  1    n     3
    2  1    o     3
    3  2    m     4
    4  2    m     4
    5  2    n     4
    6  2    n     4
    
    0 讨论(0)
提交回复
热议问题