Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Weight Distribution
#8
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.
Reply


Messages In This Thread
Weight Distribution - by 11drk9 - Mar-10-2024, 02:31 AM
RE: Weight Distribution - by Pedroski55 - Mar-10-2024, 09:12 AM
RE: Weight Distribution - by 11drk9 - Mar-10-2024, 11:44 PM
RE: Weight Distribution - by 11drk9 - Mar-11-2024, 12:00 AM
RE: Weight Distribution - by Pedroski55 - Mar-11-2024, 09:08 AM
RE: Weight Distribution - by 11drk9 - Mar-11-2024, 01:48 PM
RE: Weight Distribution - by 11drk9 - Mar-11-2024, 05:48 PM
RE: Weight Distribution - by Pedroski55 - Mar-11-2024, 06:01 PM
RE: Weight Distribution - by 11drk9 - Mar-11-2024, 06:38 PM
RE: Weight Distribution - by Pedroski55 - Mar-12-2024, 12:41 PM
RE: Weight Distribution - by 11drk9 - Mar-12-2024, 07:00 PM
RE: Weight Distribution - by Pedroski55 - Mar-13-2024, 06:08 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  distribution fit Glaucio 1 337 Apr-07-2024, 12:30 AM
Last Post: Larz60+
Information Best distribution method inovermyhead100 0 623 Jul-19-2023, 07:39 AM
Last Post: inovermyhead100
  How do I use a whl puython distribution? barryjo 6 1,927 Aug-15-2022, 03:00 AM
Last Post: barryjo
  Help with basic weight converter PythonSquizzel 3 1,529 Jun-29-2022, 02:42 PM
Last Post: deanhystad
  Python Networkx: Visualize an edge weight with a bubble/circle uvw 0 2,067 Sep-01-2021, 06:26 AM
Last Post: uvw
  Coin Toss - Distribution lasek723 6 3,266 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