Pandas Groupby How to Show Zero Counts in DataFrame

后端 未结 1 900
忘掉有多难
忘掉有多难 2020-12-20 07:15

I have the following Pandas dataframe:

Name   | EventSignupNo | Attended | Points
Smith  | 0145          | Y        | 20.24
Smith  | 0174          | Y                


        
相关标签:
1条回答
  • 2020-12-20 07:28

    You could do this with groupby + agg. For your exact output with Y and N at each level, you'd need reindex:

    g = df.groupby(['Name', 'Attended'], sort=False).Points.agg(['count', 'sum'])
    
    g
                     count    sum
    Name   Attended              
    Smith  Y             2  49.38
           N             1   0.00
    Adams  N             1   0.00
           Y             1  33.43
    Morgan Y             2  54.38
    
    idx = pd.MultiIndex.from_product([g.index.levels[0], ['Y', 'N']])
    
    idx
    MultiIndex(levels=[['Adams', 'Morgan', 'Smith'], ['N', 'Y']],
               labels=[[2, 2, 0, 0, 1, 1], [1, 0, 1, 0, 1, 0]])
    
    
    g.reindex(idx, fill_value=0)
    
              count    sum
    Smith  Y      2  49.38
           N      1   0.00
    Adams  Y      1  33.43
           N      1   0.00
    Morgan Y      2  54.38
           N      0   0.00
    
    0 讨论(0)
提交回复
热议问题