2

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.

4
  • Why 23005? typo? Commented Sep 16, 2021 at 22:09
  • can you post the actual code? that looks like pseudocode. it certainly doesn't result in a dataframe. Commented Sep 16, 2021 at 22:12
  • @Corralien : I didn't notice it. It must be a wrong number, I am getting out of the code. Thank you. Commented Sep 16, 2021 at 22:22
  • @Corralien: thank you for the code. Commented Sep 16, 2021 at 22:23

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

@irfanraorao. Does it solve your problem?
It did work. Thank you.
1

To debug the code, try to add print() statement after every step to see what the code is doing!

1 Comment

Hi @Daniele - welcome to StackOverflow! This is a debugging tip, but is not an answer to the question. You don't yet have enough reputation to comment - to get started try to find a question you can answer completely. Check out this guide to answering questions. Good luck :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.