을 어떻게 추가할 수 있습니 subtotal 행 멀티 인덱스가 데이터 프레임?

0

질문

고 그것을 찾을 수 있습니다.

여기에서의 내한 현재 데이터를 프레임:

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

은 이렇습니다.

가 합 데이터를 사용하여 프레임에 이 코드:

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

다음과 같다:

Attribute 1   Value
A             33
H             48

여기에서 내 원하는 출력을 결합하면 두:

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

은 이 같은 것을 사용하여 가능한 만 판? 감사합니다.

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

최고의 응답

1

을 유지하는 원래의 정렬을 나는 그것을 해결을 사용하여 루프 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

이렇게 당신이 원하는 출력

enter image description here

2021-11-24 04:03:23
0

한 가지 방법은 다음과 같습니다,당신은 전략적인 값으로 인해하는 알파벳 순:

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

출력:

                                     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

다른 언어로

이 페이지는 다른 언어로되어 있습니다

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