Pandas multiple grouping statistics

Ladies and Gentlemen, I would like to ask you a Pandas grouping question, which I feel is more complicated.

df = pd.DataFrame({"Date":pd.date_range(start="2018-08-17 08:10:30",periods=15,freq="s",normalize=True),"Category":list("AAAAAAAAAAAABBB"),"ActionType":[1,3,2,1,4,5,3,1,3,4,5,2,1,3,2]})

first classify by Category, sort by Date time in the category, and then continue grouping statistics by ActionType. The statistical rule of ActionType is: 1 is the beginning of each group, 2 is the end, but 2 does not necessarily appear. The result of the sample data should be that Category is divided into two groups An and B, in which there are three groups in An and a group in B

.

can you tell me how to achieve it or what ideas do you have?

Apr.22,2021

df = pd.DataFrame({"Date":pd.date_range(start='2018-08-17 08:10:30',periods=15,freq='s',normalize=True),"Category":list('AAAAAAAAAAAABBB'),"ActionType":[1,3,2,1,4,5,3,1,3,4,5,2,1,3,2]})
split = []
for idx,(ctg,act) in enumerate(zip(df.Category,df.ActionType)):
    if act == 1:
        split.append(idx)
        print(idx,ctg,act)
split.append(len(df.Category))
for i in range(len(split)-1):
    print(df.Date[split[i]:split[i+1]])

result:
0 A1
3 A1
7 A1
12 B1
0 2018-08-17 00:00:00
1 2018-08-17 00:00:01
2 2018-08-17 00:00:02
Name: Date, dtype: datetime64 [ns] > 3 2018-08-17 00:00:03
4 2018-08-17 00:00:04
5 2018-08-17 00:00:05
6 2018-08-17 00:00:06
Name: Date, Dtype: datetime64 [ns]
7 2018-08-17 00:00:07
8 2018-08-17 00:00:08
9 2018-08-17 00:00:09
10 2018-08-17 00:00:10
11 2018-08-17 00:00:11
Name: Date, dtype: datetime64 [ns]
12 2018-08-17 00:00:12
13 2018-08-17 00:00:13
14 2018-08-17 00:00:14
Name: Date, dtype: datetime64 [ns]


python3

np.vsplit( df,np.where(df.ActionType==1)[0][1:])
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1ba0ccf-11a70.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1ba0ccf-11a70.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?