I have the following dataframe which I'm wanting to create 3 new dataframes from using the values in specific columns (ppbeid, initpen and incpen) and using the unique entries in the benid and id columns:
In part of my code, below, I'm using a list of unique items in the benid column, then removing any blanks from the list. This will give me some of the column headers I want in the new dataframes, but I also want the unique ids in the id column, the aforementioned list of unique benid items and, for a couple of the new dataframes, a total column too (see last 3 Excel screenshots):
lst_benids = df_ppbens.benid.unique()
lst_benids = list(filter(None, lst_benids))
# Result is: ['PENSION', 'POST', 'PRE8', 'SPOUSE', 'RULE29', 'MOD']
I know how to achieve this in Excel using Index/Match/Match, but it's long-winded and I really want to learn how to do this in Pandas. The output should be the following 3 dataframes (which I'll then export to Excel in different worksheets):
First dataframe should be what the ppbeid column entry is, for the corresponding benid field, listed by the unique ids:
The second dataframe should be the initpen figures for those unique ids are and the specific corresponding benid, with a total column at the end:
The third and final dataframe is the same as above but instead it's got the incpen column figures for corresponding benids and a total column at the end:
Any help is much appreciated and it will help me learn something I have to do manually in Excel a lot. Being new to Pandas/Python, I'm finding it confusing navigating the documents and other resources online. Thanks



