'Pandas groupby counting values > 0

I have a pandas df of the following format

MATERIAL    DATE         HIGH    LOW
AAA       2022-01-01     10      0
AAA       2022-01-02     0       0
AAA       2022-01-03     5       2
BBB       2022-01-01     0       0
BBB       2022-01-02     10      5
BBB       2022-01-03     8       4

I am looking to transform it such that I land up with the below result

MATERIAL      HIGH_COUNT    LOW_COUNT 
AAA            2              1         
BBB            2              2         

Essentially for "HIGH_COUNT" and "LOW_COUNT" I want to count the number of occurrences that column was greater than 0, grouped by "MATERIAL". I have tried to do df.groupby(['MATERIAL']).agg<xxx> but I am unsure of the agg function to use here.

Edit:

I used

df.groupby(['MATERIAL']).agg({'HIGH':'count', 'LOW':'count}) 

but this counts even the 0 rows.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source