Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Groupby several columns and take the sum based off of categorical values within a column (Pandas)

I am looking to groupby several columns and take the sum based off of categorical values within a column.

Data

name    size    type
AA      9385    FALSE
AA      9460    FALSE
AA      9572    TRUE
AA      9680    
BB      10      TRUE
BB      10      TRUE
BB      20      FALSE
BB      20      FALSE
        
    

Desired

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

name    size    type
AA      9572    TRUE
AA      18845   FALSE
AA      9680    
BB      20      TRUE
BB      40      FALSE
BB       

Doing

df = df.groupby('name').agg({'size': 'sum', 'type': lambda x: x.value_counts().idxmax()})

However, this appears to have removed Null values. Any suggestion is appreciated.

>Solution :

Use dropna=False in groupby:

df.groupby(['name', 'type'], dropna=False, as_index=False)['size'].sum()

Output:

  name   type   size
0   AA  False  18845
1   AA   True   9572
2   AA    NaN   9680
3   BB  False     40
4   BB   True     20
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading