Q: Counting number of similar rows short python question:
I have the following df with fake data in it:

I need to count for each hour (purchase_hour column) the maximum consecutive tickets sold to a show.
meaning, I expect to see for 9 AM adel sold 2 consecutive (she sold 4, but not consecutively), and kanye didn't sell anything  consecutively.
For 10 am, adel sold 3 and kanye sold 2.
Anyone?
I created the following code, but it has it issues and it's not complete.
The main issue I can't get it to print for 10 am
i=0
count_adel, count_kanye =0,0
while i < (len(df)-1):
    if (df["purchase_hour"].iloc[i] == df["purchase_hour"].iloc[i+1] and \
        df["ticket_desc"].iloc[i] == df["ticket_desc"].iloc[i+1] and i != (len(df)-1)):
            if df["ticket_desc"][i] == "Adel L.A. Concert":
                count_adel+=1
            else:
                count_kanye+=1
    elif (df["purchase_hour"].iloc[i] != df["purchase_hour"].iloc[i+1]):
        print("adel ticket at " + str(df["purchase_hour"][i]) + " is " +str(count_adel))
        print("kanye ticket at " + str(df["purchase_hour"][i]) + " is " +str(count_kanye))
        count_adel=0
        count_kanye=0

    i=i+1


A: Here's how you can do it:
df['tmp'] = (df['ticket_desc'] != df['ticket_desc'].shift()).cumsum()
result = df.groupby(
    ['purchase_hour', 'tmp', 'ticket_desc'],
    as_index=False,
).size().drop(columns='tmp')

Let's make an example:
>>> df
  ticket_desc  purchase_hour
0           a              9
1           a              9
2           b              9
3           a              9
4           a              9
5           a             10
6           a             10
7           a             10
8           b             10
9           b             10

>>> result
   purchase_hour ticket_desc  size
0              9           a     2
1              9           b     1
2              9           a     2
3             10           a     3
4             10           b     2


A: I have used some randomly generated data that resembles your data, and used groupby+cumcum+cumcount:
# Input Data
data = pd.DataFrame({'artist':[np.random.choice(['artist 1', 'artist 2'], p=[0.55, 0.45]) for i in range(30)], 'price':np.random.randint(100,360,size=30),
                    'hour':[np.random.choice([9,10, 11]) for i in range(30)]}).sort_values(by='hour').reset_index(drop=True)

>>> data.head(10)

    artist     price hour
0   artist 1    230 9
1   artist 2    193 9
2   artist 2    114 9
3   artist 2    209 9
4   artist 1    298 9
5   artist 1    306 9
6   artist 1    153 9
7   artist 1    273 9
8   artist 2    276 9
9   artist 1    160 9

Firstly I create a new column with the cumulative count, so for each artist and hour of purchase you get the cumulative count restarting at 0 every time the condition is not satisfied
data['cond'] = np.where((data['hour']==data['hour'].shift()) & (data['artist']==data['artist'].shift()), 1, 0)
data['cond'] = data.groupby(data['cond'].eq(0).cumsum()).cumcount()

>>> data.head()

    artist  price   hour    cond
0   artist 1    230 9   0
1   artist 2    193 9   0
2   artist 2    114 9   1
3   artist 2    209 9   2
4   artist 1    298 9   0

And after that you can just groupby and get the max value (add 1 because it starts counting at 0)
data.groupby(['hour', 'artist'])[['cond']].max()+1

>>> data.head(10)

               cond
hour artist        
9    artist 1     4
     artist 2     3
10   artist 1     3
     artist 2     2
11   artist 1     2
     artist 2     2

