I have a pandas dataframe that has columns for Date, Minimum Temp, and Maximum Temp. I want to run through the dataframe and for each day, first determine the mean temperature (using min and max) if the max temp is >86; otherwise, I want it do something else to calculate the mean. Then, using the mean obtained via the first function, I want to run another function and collect the output from that in an array. I hit an error with the "truth value of a series" being ambiguous.
This is the code I've written so far:
#Function defining how to obtain the mean based on max temp
def MeanTemp(T_min, T_max):
if T_max < 86:
mean = np.mean(T_max, T_min)
else:
mean = np.mean(86, T_min)
return mean
#Function that will use the mean from the MeanTemp function
def GrowingDegreeDays(mean,base):
if mean > base:
GDD = mean-base
else:
GDD = 0
#For each row in my dataframe, I want it to perform these two functions
for Date in df:
mean = MeanTemp(T_min, T_max)
GrowingDegreeDays(mean,50)
When I run this, I get the error:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all(). It throws up this error because of the line if T_max < 86:; from some research, I think this is because it is trying to run the function on the whole column? How do I get it to just look at that value for that specific row? Or is something else going on? I am a very newbie coder, so I appreciate the user of simpler language ;)
Thanks for your help!
You are right about your error. Furthermore, try to use vectorized code to be more efficient:
clipcan avoid your conditional statements.Output:
Minimal Reproducible Example: