Mar-11-2024, 06:01 PM
Quote:Second Step: Since insurance in Level_3 group does not have any value in child_weight,
parent_weight value gets divided among all rows which has same Level_2.
All 5 rows have Financial in Level_2, how is the above supposed to work?
Did you really check the data?
import pandas as pd cols = ['Security', 'CompRating', 'Level_3', 'Level_2', 'parent_weight', 'child_weight'] # revised data datastring = """A A1 Banking Financial 4.486 3.522 B BBB3 Banking Financial 0.445 0.559 C A1 Financial_Services Financial 0.821 0.994 D A3 Financial_Services Financial 2.744 3.445 E A2 Financial_Services Financial 1.919 0.456 F A1 Insurance Financial 0.393 NULL""" lists = [s.split() for s in datastring.split('\n')] df = pd.DataFrame(lists, columns=cols) # set the number of columns to show pd.set_option('display.max_columns', 10) # add a column with a default value of 0 or set all values to 0 if df['Totals'] exists df['Totals'] = 0 # the numbers are strings, make them floats df['parent_weight'] = pd.to_numeric(df.parent_weight, errors='coerce') df['child_weight'] = pd.to_numeric(df.child_weight, errors='coerce') # calculate totals and write to Totals for Banking and Financial_Services for index in df.index: # step 1.1 make sums if df['Level_3'][index] == 'Banking': sumparent = df.loc[df['Level_3'] == 'Banking', 'parent_weight'].sum() sumchild = df.loc[df['Level_3'] == 'Banking', 'child_weight'].sum() total = df['Totals'][index] + (df['child_weight'][index] * sumparent/sumchild) df.at[index, 'Totals'] = total elif df['Level_3'][index] == 'Financial_Services': sumparent = df.loc[df['Level_3'] == 'Financial_Services', 'parent_weight'].sum() sumchild = df.loc[df['Level_3'] == 'Financial_Services', 'child_weight'].sum() total = df['Totals'][index] + (df['child_weight'][index] * sumparent/sumchild) df.at[index, 'Totals'] = total
Output: Security CompRating Level_3 Level_2 parent_weight \
0 A A1 Banking Financial 4.486
1 B BBB3 Banking Financial 0.445
2 C A1 Financial_Services Financial 0.821
3 D A3 Financial_Services Financial 2.744
4 E A2 Financial_Services Financial 1.919
5 F A1 Insurance Financial 0.393
child_weight Totals
0 3.522 4.255570
1 0.559 0.675430
2 0.994 1.113605
3 3.445 3.859526
4 0.456 0.510869
5 NaN 0.000000
Sorry, but I don't understand what you want to do with the row Insurance, please explain.