I have a dataframe containing some input values, and I trying to evaluate a parameter conditioned on the values given in a column (example below).
What I would like to obtain is shown in the figure:
How can I solve the issue below?
import numpy as np
import pandas as pd
df = pd.DataFrame.from_dict({
'x': [0,1,2,3,4],
'y': [100,100,100,100,100],
'z': [100,100,100,100,100],
})
def evaluate(input):
if input <=2:
a=4
b=6
else:
a=7
b=8
return df['x']*a+b*(df['y']+df['z'])
df['calc'] = evaluate(df['x'])
> ---------------------------------------------------------------------------
> ValueError Traceback (most recent call last)
> ~\AppData\Local\Temp/ipykernel_38760/3329748611.py in <module>
> 15 b=8
> 16 return df['x']*a+b*(df['y']+df['z'])
> 17 df['calc'] = evaluate(df['x'])
>
> ~\AppData\Local\Temp/ipykernel_38760/3329748611.py in evaluate(input)
> 8 })
> 9 def evaluate(input):
> 10 if input <=2:
> 11 a=4
> 12 b=6
>
> ~\anaconda3\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
> 1535 @final
> 1536 def __nonzero__(self):
> 1537 raise ValueError(
> 1538 f"The truth value of a {type(self).__name__} is ambiguous. "
> 1539 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
>
> ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().