DataFrame:
account_id plan_id policy_group_nbr plan_type split_eff_date splits
0 470804 8739131 Conversion732 Onsite Medical Center 1/19/2022 Bob Smith (28.2) | John Doe (35.9) | A...
1 470804 8739131 Conversion732 Onsite Medical Center 1/21/2022 Bob Smith (19.2) | John Doe (34.6) | A...
2 470809 2644045 801790 401(k) 1/18/2022 Jim Jones (100)
3 470809 2644045 801790 401(k) 1/5/2022 Other Name (50) | Jim Jones (50)
4 470809 2738854 801789 401(k) 1/18/2022 Jim Jones (100)
... ... ... ... ... ... ...
1720 3848482 18026734 24794 Accident 1/20/2022 Bill Underwood (50) | Jim Jones (50)
1721 3848482 18026781 BCSC FSA Admin 1/20/2022 Bill Underwood (50) | Jim Jones (50)
1722 3927880 19602958 Consulting Other 1/20/2022 Bill Brown (50) | Tim Scott (50)
1723 3927880 19863300 Producer Expense 5500 Filing 1/20/2022 Bill Brown (50) | Tim Scott (50)
1724 3927880 19863300 Producer Expense 5500 Filing 1/21/2022 Bill Brown (50) | Tim Scott (50)
I need to group by (account_id, plan_id, policy_group_nbr, plan_type) sorted by split_eff_date (desc), in order to remove all rows for the group but the most recent date while maintaining all columns. I can get a rank however, when attempting to pass an argument to the lambda function, I'm receiving a TypeError.
working as expected:
splits['rank'] = splits.groupby(['account_id', 'plan_id', 'policy_group_nbr', 'plan_type'])['split_eff_date'].apply(lambda x: x.sort_values().rank())
TypeError: incompatible index of inserted column with frame index
splits['rank'] = splits.groupby(['account_id', 'plan_id', 'policy_group_nbr', 'plan_type'])['split_eff_date'].apply(lambda x: x.sort_values(ascending=False).rank())
passing the axis argument didn't seem to help either... is this a simple syntax issue, or am I not understanding the function properly?