I created a pivot table that is meant to count the total number of rejections for Types A-K and their subtypes (A1-A12)
piv_table2 = df.loc[(Filter1) & (Filter2)].pivot_table(
values=["Level3"],
index=["Level1", "Level2"],
columns=None,
aggfunc="count",
margins=True,
margins_name="Total"
)
This is what a sample of my input df looks like:
WeekNumber, Level1, Level2, Level3
22, H, H1, Obs
22, H, H2, Exc
23, B, B10, Obs
23, A, A6, Obs
The week number columns are the week number, Level1 is the high level category, with Level2 being subcategories, & Level3 being "Obs" or "Exc"
piv_table2 looks like this:
# piv_table2 Output
Level3
Level1 Level2
A
A1- 175
A10- 2
A11- 10
A12- 30
...
K
K1 - 12
K11 - 12
K11 - 13
Total 5170
Essentially, I'd like to count the sub categories (A - K) along with the complete total of everything from A-K. Ideally, the output for piv_table2 would give me the total for all As, all Bs, all Cs, etc. AND then the sum of all those counts. The desired output would look something like this:
# Desired Output with Level1 Totals & Complete Totals
Level3
Level1 Level2
A - 617
A1- 175
A10- 2
A11- 10
A12- 30
...
K - 12
K1 - 10
K11 - 1
K11 - 1
Total 5170
The totals for A & K, along with the totals for EVERYTHING from A-K are shown in the desired output.
I tried using pivot_table2.merge(pivot_table) & vice versa with no success.