I've been wrestling with this for a couple of days now, despite a lot of searching. I've come across a number of similar problems, but I've not been able to make any of the solutions work for me.
Here's my starting dataframe:
data = {
"account_id": ["1001", "1001", "1002", "1002", "1002", "1002", "1002", "1003", "1003", "1003"],
"data_type": ["initial_balance", "payment", "payment", "initial_balance", "payment", "payment", "payment", "payment", "initial_balance", "payment"],
"transaction_date": ["2022-04-01", "2022-04-14", "2022-03-01", "2022-04-02", "2022-04-13", "2022-05-01", "2022-05-03", "2022-03-13", "2022-04-10", "2022-04-20"],
"amount": [100, -20, -30, 200, -20, -20, -20, -10, 150, -50],}
Which, once in Pandas becomes:
I'm looking to group by account_id and remove any entry before the entry with data_type = "initial_balance". Once I've got this, I can then cumsum over the remaining group rows to get to the current balance. So the desired outcome (including the cumsum "account_balance" column) is:
Desired result including the cumsum "account_balance" column
I've tried the following:
df.groupby("account_id").filter(lambda x:x["transaction_date"]>=x[x["data_type"]=="initial_balance"]["transaction_date"])
but this just produces the error: ValueError: Can only compare identically-labelled Series objects
I hope I've provided enough information for someone to help. Thanks very much in advance for your help.