import pandas as pd
def calculate_hierarchy_rollup(
df,
emp_col='Employee ID',
sup_col='Supervisor ID',
count_col='PRCount',
output_col='PRCountSum'
):
# Keep one row per employee
temp_df = (
df[[emp_col, sup_col, count_col]]
.drop_duplicates(subset=[emp_col])
.copy()
)
# Build hierarchy
children_map = (
temp_df.groupby(sup_col)[emp_col]
.apply(list)
.to_dict()
)
count_map = (
temp_df.set_index(emp_col)[count_col]
.to_dict()
)
memo = {}
def get_rollup_sum(emp_id):
if emp_id in memo:
return memo[emp_id]
total = count_map.get(emp_id,0)
for child in children_map.get(emp_id,[]):
total += get_rollup_sum(child)
memo[emp_id] = total
return total
result_df = df.copy()
result_df[output_col] = (
result_df[emp_col]
.apply(get_rollup_sum)
)
return result_df