1

Thanks for looking into my questions! To sum up, I run the code in a loop ("for year in 2019 to 2028"), in each iteration, I have a constant value "temperature" from another dataframe that only changes with year. I am trying to modify the value of a column called "forecast" based on that. For example, in the first iteration as year is 2019, I want forecast = temperature, and only the rows with year=2019 would be changed, and so on with the following years

I tried to use if to set the condition, but there is error.

for year in range (2019,2029):
    temperature=(
        df_Ckt.loc[df_Ckt['Circuit Key']==circuit,year].reset_index(drop=True))
    if df['year'] == year:
       df['forecast_year']=year_value

The errors are as below:

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I know that I can avoid using if-else using functions like loc, but does loc only returns the value in a dataframe instead of constant values?

Yuchen Chen
  • 21
  • 1
  • 4
  • Try changing condition to `(df['year'] == year).bool()`. TBH I have no idea what I'm talking about, found some related info here: https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o. It seems normal python true values cannot be used, and you need to cast them somehow. – alx Jun 23 '19 at 00:24
  • @alx That's incorrect, series cannot be used in if conditions because ifs are meant to work with simple scalar values. – cs95 Jun 23 '19 at 00:25

0 Answers0