How do I correctly apply group by on pandas dataframe?

COL:SPM  COL:BS COL:PL2    COL:PL3  sum
   CCTC       BG      OP      OTH    1
   CCTC       BG      tech    OTH    3
   CCTC       BG      OP      OTH    5
   CCTC       BG      Info    OTH    10

I am applying groupby as I want my data to be grouped on all these columns spm,bs,pl2,pl3:

Expected Result:

COL:SPM  COL:BS COL:PL2    COL:PL3  sum
   CCTC       BG      OP      OTH    6
   CCTC       BG      tech    OTH    3
   CCTC       BG      Info    OTH    10

The result I am getting:

  COL:SPM  COL:BS COL:PL2    COL:PL3  sum
       CCTC       BG      OP      OTH    some unverified integer value

df.groupby(['SPM','BS','PL2','PL3'])['sum'].sum().reset_index()

I am not understanding why I am getting the result wrong? I have searched on the net and I have been unsuccessful in finding the solution

1 answer

  • answered 2021-10-24 20:13 Corralien

    Can you try this minimal example code. Does it work?

    df = pd.DataFrame({'SPM': ['CCTC', 'CCTC', 'CCTC', 'CCTC'],
                       'BS': ['BG', 'BG', 'BG', 'BG'],
                       'PL2': ['OP', 'tech', 'OP', 'Info'],
                       'PL3': ['OTH', 'OTH', 'OTH', 'OTH'],
                       'sum': [1.0, 3.0, 5.0, 10]})
    
    out = df.groupby(['SPM', 'BS', 'PL2', 'PL3']).sum().reset_index()
    

    Output:

    >>> df
        SPM  BS   PL2  PL3  sum
    0  CCTC  BG    OP  OTH    1
    1  CCTC  BG  tech  OTH    3
    2  CCTC  BG    OP  OTH    5
    3  CCTC  BG  Info  OTH   10
    
    >>> out
        SPM  BS   PL2  PL3  sum
    0  CCTC  BG  Info  OTH   10
    1  CCTC  BG    OP  OTH    6
    2  CCTC  BG  tech  OTH    3
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum