Jak mogę dodać wiersz sumy częściowej w ramkę danych z kilkoma indeksami?

0

Pytanie

I zrób tak, żeby to też wyglądało pięknie.

Oto mój obecny ramka danych:

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

Coś w tym stylu.

Mam ramkę danych sum, wykorzystujący ten kod:

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

jak należy:

Attribute 1   Value
A             33
H             48

Oto mój pożądany wynik, który łączy te dwa:

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

Czy możliwe jest coś takiego, przy użyciu tylko pandy? Dziękuję.

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

Najlepsza odpowiedź

1

Aby zachować pierwotną sortowanie, bym uznał ją za pomocą pętli 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

To daje pożądany wynik

enter image description here

2021-11-24 04:03:23
0

Oto jeden ze sposobów, trzeba być strategicznie są ustawione w stosunku do wartości z powodu alfabetyczne sortowanie:

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()

Wyjście:

                                     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

W innych językach

Ta strona jest w innych językach

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