dimanche 22 octobre 2017

Calculating a Streak from a column in Python

I have a df that has a Basket column of hits H and misses H. I am calculating a streak from this column. My df looks like this,

    vs   game quarter   Basket      
0   ORL    1    1           H
1   ORL    1    1           M
2   ORL    1    1           M
3   ORL    1    1           H
4   ORL    1    1           H
5   ORL    1    1           M
6   ORL    1    1           M
7   ORL    1    1           M
8   ORL    1    1           M
9   ORL    1    2           H
10  ORL    1    2           H
11  ORL    1    2           H
12  ORL    1    2           M

This is how, I have my function,

count = 0
def count_hit(x):
    global count
    if x == 'H':
        count += 1     
    else:
        count = 0
    return count

After this, I created a variable Streak with above function as function does I get the

Streak = df['basket'].apply(count_hit)
print(Streak)
>> 1 0 0 1 2 0 0 0 0...

However, I need my Streak like this,

1 0 2 0 0 0.. 

Here, for example, first Streak 1 is a H followed by miss M (Indexed 1 through 1 and there is a 0 after because of miss in 2nd index). Second Streak is 2 that goes from index 3 through 5.

How should I tweak my function here to calculate like that. I want to go to the next M miss as soon as H hit ends to calculate a streak.

Any help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire