Mar-12-2024, 07:00 PM
Thanks for the code. The output matches with the expected values. I agree with the groupby function to be used since I will be applying to a larger set of data. I am just struggling to get the second step incorporated in the below code.
data = {
'Security': ['A', 'B', 'C', 'D', 'E', 'F'],
'CompRating': ['A1', 'BBB3', 'A1', 'A3', 'A2', 'A1'],
'Level_3': ['Banking', 'Banking', 'Financial Services', 'Financial Services', 'Financial Services', 'Insurance'],
'Level_2': ['Financial', 'Financial', 'Financial', 'Financial', 'Financial', 'Financial'],
'parent_weight': [4.486, 0.445, 0.821, 2.744, 1.919, 0.393],
'child_weight': [3.522, 0.559, 0.994, 3.445, None, None]
}
df = pd.DataFrame(data)
def redistribute_parent_weight(group):
total_child_weight = group['child_weight'].sum()
total_parent_weight = group['parent_weight'].sum()
total_child_weight_2 = group.groupby('Level_2')['child_weight'].sum()
if pd.notnull(total_child_weight):
group['redistributed_parent_weight_1'] = (total_parent_weight * group['child_weight']) / total_child_weight
##group['redistributed_parent_weight_2'] = (group['parent_weight'] * group['child_weight']) / total_child_weight_2
return group
df = df.groupby(['Level_3']).apply(redistribute_parent_weight)
data = {
'Security': ['A', 'B', 'C', 'D', 'E', 'F'],
'CompRating': ['A1', 'BBB3', 'A1', 'A3', 'A2', 'A1'],
'Level_3': ['Banking', 'Banking', 'Financial Services', 'Financial Services', 'Financial Services', 'Insurance'],
'Level_2': ['Financial', 'Financial', 'Financial', 'Financial', 'Financial', 'Financial'],
'parent_weight': [4.486, 0.445, 0.821, 2.744, 1.919, 0.393],
'child_weight': [3.522, 0.559, 0.994, 3.445, None, None]
}
df = pd.DataFrame(data)
def redistribute_parent_weight(group):
total_child_weight = group['child_weight'].sum()
total_parent_weight = group['parent_weight'].sum()
total_child_weight_2 = group.groupby('Level_2')['child_weight'].sum()
if pd.notnull(total_child_weight):
group['redistributed_parent_weight_1'] = (total_parent_weight * group['child_weight']) / total_child_weight
##group['redistributed_parent_weight_2'] = (group['parent_weight'] * group['child_weight']) / total_child_weight_2
return group
df = df.groupby(['Level_3']).apply(redistribute_parent_weight)