Created
October 11, 2024 02:18
-
-
Save luisdelatorre012/d4d42fdc147570d5eeef67b58719b425 to your computer and use it in GitHub Desktop.
prorate proportionally
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def prorate_proportionally(df: pd.DataFrame, group: pd.DataFrame): | |
# Get the total tonnage values from the first row | |
total_tonnage_moved_contract = group.iloc[0]["tonnage_moved_contract"] | |
total_tonnage_moved_spot = group.iloc[0]["tonnage_moved_spot"] | |
total_tonnage_moved_unknown = group.iloc[0]["tonnage_moved_unknown"] | |
# Calculate the total commitment | |
total_commitment = group["tonnage_committed"].sum() | |
# Compute proration factors for each row | |
proration = group["tonnage_committed"] / total_commitment | |
# Assign prorated tonnage values to each row in the group | |
df.loc[group.index, "tonnage_moved_contract"] = proration * total_tonnage_moved_contract | |
df.loc[group.index, "tonnage_moved_spot"] = proration * total_tonnage_moved_spot | |
df.loc[group.index, "tonnage_moved_unknown"] = proration * total_tonnage_moved_unknown | |
return df |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment