Following on from my previous question (thanks to those responding) I'm stuck again in achieving what I suspect is possible using a groupby in Pandas. Here's what I'm trying to achieve. With the following example dataframe:
data_initial = {
"account_id": ['1001', '1001', '1001', '1002', '1002', '1002', '1002', '1002', '1002', '1002', '1002', '1002', '1002', '1003', '1003', '1003', '1003', '1003', '1003',],
"data_type": ['payment', 'payment', 'payment', 'payment', 'payment', 'plan', 'payment', 'plan', 'plan', 'payment', 'payment', 'payment', 'payment', 'payment', 'plan', 'payment', 'payment', 'payment', 'payment',],
"transaction_date": ['2022-04-01', '2022-04-12', '2022-05-02', '2022-02-02', '2022-03-01', '2022-03-15', '2022-04-01', '2022-04-01', '2022-04-13', '2022-04-26', '2022-05-01', '2022-05-04', '2022-05-10', '2022-03-10', '2022-03-25', '2022-04-05', '2022-04-16', '2022-04-24', '2022-05-05',],
"amount": ['-50', '-40', '-60', '-30', '-25', '250', '-50', '200', '200', '-25', '-25', '-25', '-25', '-20', '100', '-25', '-25', '-25', '-25',],}

I'm looking to, effectively, groupby the account_id and then apply the following logic:
IF
data_typeis "payment" AND {account_idhas nodata_type= "plan" OR thetransaction_dateof the record is BEFORE anydata_type= "plan" record} then new columnclassification= "receipt_not_plan_related"IF
data_typeis "payment" AND {account_idhas adata_type= "plan" ANDtransaction_dateis AFTER anydata_type= "plan" record} then new columnclassification= "receipt_on_plan"IF
data_typeis "plan" is the only instance of "plan" then new columnclassification= "only"IF
data_typeis "plan" AND is the FIRST instance of "plan" then new columnclassification= "initial"IF
data_typeis "plan" AND is NOT the FIRST and NOT the LAST instance of "plan" then new columnclassification= "expired"IF
data_typeis "plan" AND is the LAST instance of "plan" then new columnclassification= "current"
The result, therefore, for the example dataframe would be as follows:

Thanks again, in advance, to anyone who can help out. Much appreciated.