0

When I print the following it selects the correct values: df[(df['Pdem']>0)]. However, I am not able to work this into an if condition (without the ValueError: The truth value of a DataFrame is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). - error) that sets the constraints at the right timeslots. I do not want any() or all() as i want the decision to be made per row of the dataframe.

I would like to set different constraints for Pb in Gurobi per timeslot based on the size of Pdem per timeslot (T = total hours). I have tried various things such as loc (shown below), iterrows and regular boolean expressions.

Any advice would be greatly appreciated.

UPDATE: Following did the trick

model.addConstrs(((Pgen [t] - Pdem[t]) < 0) >> 0 <= P_grid_b[t] <= Pgridmax for t in range(T)) model.addConstrs((Pdem[t] == 0) >> 0 <= P_grid_b[t] <= 0 for t in range(T))

KVA
  • 49
  • 7
  • I need some more clarification. If you want to pass the logical to an `if` conditional, it has to be a boolean (i.e. you have to use `any()`, `all()` etc.). If you want to make the decision per row, the best idea i can think of, is to iterate over the rows and set the constraint based on the condition of each row. However, I *FEEL* there is a vectorized way of doing this, perhaps you are searching for that way.... – SpaceMonkey55 Apr 19 '19 at 07:16
  • I feel that this is what i want: 'is to iterate over the rows and set the constraint based on the condition of each row'. The if just needs to check IF a certain value in a dataframe is of a certain size, before consecutive actions (the indented lines) are executed. How would you formulate that? – KVA Apr 19 '19 at 08:34
  • It appears that you already answered your own question. If you are trying to iterate over rows, have a look at [this](https://stackoverflow.com/questions/16476924/how-to-iterate-over-rows-in-a-dataframe-in-pandas) link. If you think that is what you wanted to do, I would recommend **deleting this question**, to minimize the pollution in stackoveflow. If you still need a solution, ask here – SpaceMonkey55 Apr 19 '19 at 11:20
  • It was just a workaround not a general solution i'm currently trying to set op the following without succes: (P_bat_disch is a decision variable and the eff(iciencies) are parameters. I would like the model to check the dec. var. per step and set the eff accordingly. `if P_bat_disch [t] >= 0: eff_disch = 0.1 # eff_ch = 0.1 # elif P_bat_disch[t] >= 0.2: eff_disch = 0.3 # eff_ch = 0.3# else: eff_disch = 0.8 # eff_ch = 0.8 #` – KVA Apr 23 '19 at 15:59
  • Thanks for following up. Would you mind updating your question with your findings, what you tried (including the code you removed) and a sample input dataframe and also an expected output dataframe/array. That way potential answerers have a good a starting point and you improve the chance of someone picking it up and providing some good answers. There is ambiguity in your question which can cause it to get downvoted. – SpaceMonkey55 Apr 23 '19 at 16:39
  • Also, the logical condition you present here is illogical. Your second condition of `>= 0.2` will never get triggered. The first one will always be `True` if the second one is `True`, thus the second one will never activate, only the first and third (i.e. the `else`) block will be activated. – SpaceMonkey55 Apr 23 '19 at 16:58

1 Answers1

0

Maybe you are looking for the apply method.

import pandas as pd
import numpy as np

np.random.seed(42)
df = pd.DataFrame(np.random.uniform(-1.0,1.0,(10,2)), columns= ["a", "b"])

def my_func_disch(x):
    if x >= 0 and x < 0.2:
        eff_disch = 0.1
    elif x >= 0.2:
        eff_disch = 0.3
    else:
        eff_disch = 0.8

    return eff_disch

df["eff_disch"] = df.a.apply(my_func_disch)
SpaceMonkey55
  • 438
  • 4
  • 14