You can do the cumprod after set_index
s = df.set_index('id').eq('y').cumprod(axis = 1).sum(axis = 1)
s = s.reset_index(name='count_y')
s
Out[33]:
id count_y
0 1 2
1 2 1
2 3 4
If you are a beginner and want to know more about Python the do check out the python for data science course