Skip to content

Instantly share code, notes, and snippets.

@luisdelatorre012
Created October 11, 2024 02:18
Show Gist options
  • Save luisdelatorre012/d4d42fdc147570d5eeef67b58719b425 to your computer and use it in GitHub Desktop.
Save luisdelatorre012/d4d42fdc147570d5eeef67b58719b425 to your computer and use it in GitHub Desktop.
prorate proportionally
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