Come posso aggiungere una riga del subtotale per un multi indice dataframe?

0

Domanda

E renderlo sembra troppo bello.

Ecco il mio attuale frame di dati:

Attribute 1     Attribute 2   Attribute 3       Value
A               B             D                 10
                              E                 11
                C             F                 12

H               B             D                 10
                              E                 11
                C             F                 12
                              G                 15

Qualcosa di simile a questo.

Ho la somma frame di dati utilizzando questo codice:

df_sum = df.groupby('Attribute 1').sum()

come segue:

Attribute 1   Value
A             33
H             48

Ecco il mio output desiderato che unisce i due:

Attribute 1     Attribute 2   Attribute 3       Value
A               B             D                 10
                              E                 11
                C             F                 12

Subtotal for A                                  33

H               B             D                 10
                              E                 11
                C             F                 12
                              G                 15
Subtotal for H                                  48

È qualcosa di simile a questo è possibile utilizzando solo i panda? Grazie.

pandas python
2021-11-24 03:40:02
2

Migliore risposta

1

Per mantenere l'originale ordinamento vorrei risolverlo utilizzando un ciclo groupby

import pandas as pd

df = pd.DataFrame({
    'Attribute1': ['A', 'A', 'A', 'H', 'H', 'H', 'H'],
    'Attribute2': ['B', 'B', 'C', 'B', 'B', 'C', 'C'],
    'Attribute3': ['D', 'E', 'F', 'D', 'E', 'F', 'G'],
    'Value': [10, 11, 12, 10, 11, 12, 15]
})
df = df.groupby(['Attribute1', 'Attribute2', 'Attribute3']).sum()

df_out = []  # init output list
for index, df_sub in df.groupby(level=0):  # loop groupby level 0
    df_sub = df.groupby('Attribute1').sum().reset_index()  # get subtotal and reset index
    df_sub['Attribute1'] = df_sub['Attribute1'].replace({index: f"{index}_subtotal"})  # rename index value to include subtotal
    df_sub['Attribute2'] = ''  # dummy value for Attribute 2
    df_sub['Attribute3'] = ''  # dummy value for Attribute 3
    df_sub = df_sub.groupby(['Attribute1', 'Attribute2', 'Attribute3']).sum()  # match groupby structure so we can use append
    df_out.append(df.loc[index:index].append(df_sub))  # select current index value and append subtotal
df_out = pd.concat(df_out)  # merge list to DataFrame

Questo vi dà l'output desiderato

enter image description here

2021-11-24 04:03:23
0

Qui è un modo, devi essere strategico con valori a causa di ordinamento alfabetico:

df_sum=df.groupby('Attribute 1').sum()

df_sum['Attribute 2'] = 'Sub'
df_sum['Attribute 3'] = 'Total'

df_sum = df_sum.set_index(['Attribute 2', 'Attribute 3'], append=True)
pd.concat([df, df_sum]).sort_index()

Output:

                                     Value
Attribute 1 Attribute 2 Attribute 3       

    A           B           D               10
                            E               11
                C           F               12
                Sub         Total           33
    H           B           D               10
                            E               11
                C           F               12
                            G               15
                Sub         Total           48
2021-11-24 03:53:43

In altre lingue

Questa pagina è in altre lingue

Русский
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................