dimanche 19 juillet 2020

Pandas count number in one column based off a condition in a different column

I need to count the number of times id occurs given the condition that enroll > 0. This is what I have so far...Any thoughts on how to do this? Thanks!

raw_data = [['a', '0'], ['a', '0'], ['a', '1'], ['b', '0'], ['b', '0.5'], ['c', '0'], ['c', '0']]
df = pd.DataFrame(raw_data, columns = ['id', 'enroll'])
df

enter image description here

def countidsperenroll():
    for i in df['id']:
        if (enroll>0):
            return value.count()
        continue 

The result should be a table with: the values:

3
2
0

because there were 3 'a' ids and there was an enroll> 0 with one of the 'a' ids. And because there were 2 'b' ids and there was an enroll > 0 with one of the 'b' ids. No 'enroll' for the 'c' id, So that gets a 0.

Aucun commentaire:

Enregistrer un commentaire