Mar-10-2024, 02:31 AM
Provide a python function for the below case and add a column
1) Sum parent weight based on Level_3 and distribute the values based on values in child_weight column for all the rows in Level_3
2) If there is no value in child_weight for a particular group in level_3 then re-distribute across all rows in level_2 within the same group
3) Values in Total column is the expected value after running the function.
Sample DF:
Security CompRating Level_3 Level_2 parent_weight child_weight Total
A A1 Banking Financial 4.486 3.522 4.419
B BBB3 Banking Financial 0.445 0.559 0.701
C A1 Financial Services Financial 0.821 0.994 1.274
D A3 Financial Services Financial 2.744 3.445 4.415
E A2 Financial Services Financial 1.919
F A1 Insurance Financial 0.393
1) Sum parent weight based on Level_3 and distribute the values based on values in child_weight column for all the rows in Level_3
2) If there is no value in child_weight for a particular group in level_3 then re-distribute across all rows in level_2 within the same group
3) Values in Total column is the expected value after running the function.
Sample DF:
Security CompRating Level_3 Level_2 parent_weight child_weight Total
A A1 Banking Financial 4.486 3.522 4.419
B BBB3 Banking Financial 0.445 0.559 0.701
C A1 Financial Services Financial 0.821 0.994 1.274
D A3 Financial Services Financial 2.744 3.445 4.415
E A2 Financial Services Financial 1.919
F A1 Insurance Financial 0.393