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

Pandas – get subtotal of some rows

There is DataFrame as:

    host  status count
0   ast2  0      1
1   ast2  1      2
2   ast2  2      3
3   ast3  0      4
4   ast3  1      5
5   ast3  2      6
6   ast9  0      7
7   ast9  2      8

How to get sub-total of rows, where status is only 0 or 1 not 2?
So result should be:

    host  status count
0   ast2  0      3
1   ast2  2      3
2   ast3  0      9
3   ast3  2      6
4   ast9  0      7
5   ast9  2      8

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

>Solution :

You can replace 1 to 0 for status column and then aggregate sum:

df1 = (df.assign(status = df['status'].mask(df['status'].eq(1), 0))
        .groupby(['host','status'], as_index=False)['count']
        .sum())
print (df1)
   host  status  count
0  ast2       0      3
1  ast2       2      3
2  ast3       0      9
3  ast3       2      6
4  ast9       0      7
5  ast9       2      8

Alternative solution:

df1 = (df.replace({'status':{1:0}})
         .groupby(['host','status'], as_index=False)['count']
         .sum())
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