Я создал фиктивный фрейм данных df. Ниже вы можете обратиться к логике для получения счетчика для шаблона состояния.
In [109]: status = 'new,filled,partial,cancelled'.split(',')
In [102]: df = pd.DataFrame( [ [ random.randint(1,25), random.randint(100, 200), status[random.randint(0,3)] ] for _ in range(50) ], columns=['order_id','timestamp' ,'status'])
In [103]: df.head(10)
Out[103]:
order_id timestamp status
0 20 120 new
1 9 118 cancelled
2 16 125 partial
3 9 124 cancelled
4 2 190 filled
5 3 185 partial
6 5 162 filled
7 21 101 new
8 25 115 filled
9 14 141 filled
In [104]: df_grouped = df.groupby('order_id', as_index=False)
In [105]: def status_transition_with_timestamp(each_grouped_df):
...: sorted_df = each_grouped_df.sort_values('timestamp', ascending=True)
...: concatenated_transition = ','.join(sorted_df['status'])
...: return concatenated_transition
...:
In [106]: result = df_grouped['status'].agg(status_transition_with_timestamp)
In [107]: result.head(10)
Out[107]:
order_id status
0 1 filled
1 2 filled,cancelled
2 3 partial,cancelled,partial
3 4 filled,new,cancelled
4 5 filled,cancelled
5 6 new
6 7 filled
7 9 partial,cancelled,cancelled
8 10 cancelled,new
9 11 new,partial
In [108]: result.groupby('status').count()
Out[108]:
order_id
status
cancelled,new 1
filled 4
filled,cancelled 2
filled,new,cancelled 1
filled,partial,partial 1
new 2
new,cancelled 2
new,filled 1
new,new,filled 1
new,new,new,partial,partial,cancelled,new 1
new,partial 1
partial 1
partial,cancelled,cancelled 1
partial,cancelled,partial 1
partial,partial 1
partial,partial,new,partial,new 1