Including missing combinations of values in a pandas groupby aggregation

前端 未结 1 1359
走了就别回头了
走了就别回头了 2021-02-10 06:31

Problem

Including all possible values or combinations of values in the output of a pandas groupby aggregation.

Example

相关标签:
1条回答
  • 2021-02-10 06:51

    You can use unstack with stack:

    print(example_df.groupby(['User', 'Code']).Subtotal.sum()
                    .unstack(fill_value=0)
                    .stack()
                    .reset_index(name='Subtotal'))
      User  Code  Subtotal
    0    a     1         1
    1    a     2         1
    2    b     1         1
    3    b     2         1
    4    c     1         2
    5    c     2         0
    

    Another solution with reindex by MultiIndex created from_product:

    df = example_df.groupby(['User', 'Code']).Subtotal.sum()
    mux = pd.MultiIndex.from_product(df.index.levels, names=['User','Code'])
    print (mux)
    MultiIndex(levels=[['a', 'b', 'c'], [1, 2]],
               labels=[[0, 0, 1, 1, 2, 2], [0, 1, 0, 1, 0, 1]],
               names=['User', 'Code'])
    
    print (df.reindex(mux, fill_value=0).reset_index(name='Subtotal'))
      User  Code  Subtotal
    0    a     1         1
    1    a     2         1
    2    b     1         1
    3    b     2         1
    4    c     1         2
    5    c     2         0
    
    0 讨论(0)
提交回复
热议问题