I am trying to apply the loop to select the specific values to use in the equation. I have the following data set
stock | Demand |
---|---|
1585 | -1677 |
2305 | 20 |
34215 | -38968 |
30 | 70 |
10967 | -4737 |
I am applying the following code
for i in Demand:
if i > df['Stock'].all():
Order = Demand
elif i < 0 :
Order = df['Stock'] - Demand
I want the following list
Order |
---|
3262 |
23005 |
73183 |
70 |
15704 |
But I am getting the following list
Order |
---|
3262 |
23005 |
73183 |
-39 |
15704 |
I am unable to figure out what is wrong with the loop I am applying here. Thank you.
CodePudding user response:
To debug the code, try to add print() statement after every step to see what the code is doing!
CodePudding user response:
I tried to understand your logic and fixed errors. Is it what you expect?
df['Order'] = np.where(df['Demand'] < 0,
df['stock'] - df['Demand'],
df[['stock', 'Demand']].max(1))
Output:
>>> df
stock Demand Order
0 1585 -1677 3262
1 2305 20 2305
2 34215 -38968 73183
3 30 70 70
4 10967 -4737 15704