Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Weight Distribution
#11
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)
Reply
#12
Post a bigger Excel then maybe I can help.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  distribution fit Glaucio 1 237 Apr-07-2024, 12:30 AM
Last Post: Larz60+
Information Best distribution method inovermyhead100 0 565 Jul-19-2023, 07:39 AM
Last Post: inovermyhead100
  How do I use a whl puython distribution? barryjo 6 1,798 Aug-15-2022, 03:00 AM
Last Post: barryjo
  Help with basic weight converter PythonSquizzel 3 1,421 Jun-29-2022, 02:42 PM
Last Post: deanhystad
  Python Networkx: Visualize an edge weight with a bubble/circle uvw 0 2,010 Sep-01-2021, 06:26 AM
Last Post: uvw
  Coin Toss - Distribution lasek723 6 3,124 Oct-04-2020, 01:36 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020