I have for loop written in pandas, with simple algebric function to create new columns. But they are required to be looped over a large range. How can I create an alternative as right now it takes hours and hours to execute. Please look at the code below
My Data looks like this
Org Oracle Item # OH SO OH-SO OO-Ven OO-Internal Available OH Year Week Incoming Inventory Week End Inv Sales History Weekly Fcst Receipt Qty Booked Qty Date Wk Start Date Today Next 7 Days Next 14 Days Next 4 Weeks Next 90 Days DC Hub DC Hub Oracle Item Vendor Org_Item Hub_Item Target Inventory Level Safety Stock Stock Upper Limit Projected Receipt Closing Inventory Expected Shortage Expected Excess
0 811 000033 0.0 0.0 0.0 0.0 0.0 0.0 2022.0 23.0 0.0 0.0 0.0 0.187326 0.0 0.0 2022-06-10 2022-06-06 2022-06-10 2022-06-17 2022-06-24 2022-07-08 2022-09-08 811 812 N 000033 104 811_000033 812_000033 0.995938 0.374652 1.424657 0.000000 0.0 0.187326 0.0
1 811 000033 0.0 0.0 0.0 0.0 0.0 0.0 2022.0 24.0 0.0 0.0 0.0 0.187326 0.0 0.0 2022-06-17 2022-06-13 2022-06-10 2022-06-17 2022-06-24 2022-07-08 2022-09-08 811 812 N 000033 104 811_000033 812_000033 0.931928 0.374652 1.419416 0.000000 0.0 0.187326 0.
# Calculate Projected Inventory, Shortage
for i in range(len(x)):
if i == 0:
x.iloc[i]["Projected Receipt"] = 0
x.iloc[i]["Closing Inventory"] = max(0,x.iloc[i]["Available OH"] + x.iloc[i]["Incoming Inventory"] + x.iloc[i]["Projected Receipt"] - x.iloc[i]["Weekly Fcst"])
x.iloc[i]["Expected Shortage"] = abs(min(0,x.iloc[i]["Available OH"] + x.iloc[i]["Incoming Inventory"] + x.iloc[i]["Projected Receipt"]- x.iloc[i]["Weekly Fcst"]))
x.iloc[i]["Expected Excess"] = max(0,(x.iloc[i]["Closing Inventory"] - x.iloc[i]["Stock Upper Limit"]))
elif x.iloc[i]["Org_Item"] != x.iloc[i-1]["Org_Item"]:
x.iloc[i]["Projected Receipt"] = 0
x.iloc[i]["Closing Inventory"] = max(0,x.iloc[i]["Available OH"] + x.iloc[i]["Incoming Inventory"] + x.iloc[i]["Projected Receipt"]- x.iloc[i]["Weekly Fcst"])
x.iloc[i]["Expected Shortage"] = abs(min(0,x.iloc[i]["Available OH"] + x.iloc[i]["Incoming Inventory"] + x.iloc[i]["Projected Receipt"]- x.iloc[i]["Weekly Fcst"]))
x.iloc[i]["Expected Excess"] = max(0,(x.iloc[i]["Closing Inventory"] - x.iloc[i]["Stock Upper Limit"]))
else:
if x.iloc[i]["Date"]<x.iloc[i]["Next 14 Days"]:
x.iloc[i]["Projected Receipt"] = 0
else:
x.iloc[i]["Projected Receipt"] = max(0,(x.iloc[i]["Target Inventory Level"] + x.iloc[i]["Weekly Fcst"] - x.iloc[i-1]["Closing Inventory"] - x.iloc[i]["Incoming Inventory"]))
x.iloc[i]["Closing Inventory"] = max(0,x.iloc[i-1]["Closing Inventory"] + x.iloc[i]["Incoming Inventory"] + x.iloc[i]["Projected Receipt"] - x.iloc[i]["Weekly Fcst"])
x.iloc[i]["Expected Shortage"] = abs(min(0,x.iloc[i-1]["Closing Inventory"] + x.iloc[i]["Incoming Inventory"] + x.iloc[i]["Projected Receipt"]- x.iloc[i]["Weekly Fcst"]))
x.iloc[i]["Expected Excess"] = max(0,(x.iloc[i]["Closing Inventory"] - x.iloc[i]["Stock Upper Limit"]))
x.head()