0

I have this seemingly simple bit of code in which I calculate the level of fuel for fuel cell operation. If I execute the code line by line I get valid results but when I try to run the whole conditional statement I get the "ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()." I've run out of ideas for solve this problem, any help is very welcomed.

fuel=pd.DataFrame()
if df3['DCS1'].tail(1)==1:
    fuel['fuel left']=56-(df3['Methanol consumed'].tail(1))
else:
    fuel['fuel left']=28-(df3['Methanol consumed'].tail(1))```
blooregard
  • 59
  • 4
  • 1
    Does this answer your question? [Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()](https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o) – Guy Jan 08 '20 at 10:06

1 Answers1

0

tail(1) returns a series with a single element. Comparing a series to a scalar produces another series of True/False. This second series is what got you into trouble: despite it being single-element, pandas does not want to decide the truth value for you.

You can produce the truth value explicitly:

if np.all(df3['DCS1'].tail(1)==1):
    # do things

Or extract the last element of the column as a scalar:

if df['DCS1'].iloc[-1] == 1:
    # do things
Code Different
  • 90,614
  • 16
  • 144
  • 163
  • i forced it through by making fuel=float(df3['DCS1'].tail(1)) so pandas had to compare a float. it works this way but thank you very much for your help – blooregard Jan 09 '20 at 11:10