Python - Pandas subtotals on groupby

后端 未结 1 1955
一整个雨季
一整个雨季 2020-12-10 23:34

here\'s a sample of the data i m using :

SCENARIO    DATE    POD         AREA    IDOC    STATUS  TYPE
AAA   02.06.2015    JKJKJKJKJKK 4210    713375  51              


        
相关标签:
1条回答
  • 2020-12-11 00:06

    Use:

    #if necessary convert TYPE column to string
    df['TYPE'] = df['TYPE'].astype(str)
    df = df.groupby(["SCENARIO", "STATUS", "TYPE"])['TYPE'].count()
    
    #aggregate sum by first 2 levels
    df1 = df.groupby(["SCENARIO", "STATUS"]).sum()
    #add 3 level of MultiIndex 
    df1.index = [df1.index.get_level_values(0),
                df1.index.get_level_values(1),
                ['Total'] * len(df1)]
    
    #thanks MaxU for improving
    #df1 = df1.set_index(np.array(['Total'] * len(df1)), append=True) 
    
    print (df1)
    SCENARIO  STATUS       
    AAA       51      Total    3
              53      Total    1
    BBB       51      Total    1
    CCC       51      Total    1
    Name: TYPE, dtype: int64
    

    #join together and sorts
    df = pd.concat([df, df1]).sort_index(level=[0,1])
    print (df)
    SCENARIO  STATUS  TYPE 
    AAA       51      1        2
                      9        1
                      Total    3
              53      228      1
                      Total    1
    BBB       51      43       1
                      Total    1
    CCC       51      187      1
                      Total    1
    Name: TYPE, dtype: int64
    
    0 讨论(0)
提交回复
热议问题