I have the following DataFrames:
example = pd.DataFrame({"dirr":[1,0,-1,-1,1,-1,0],
"value": [125,130,80,8,150,251,18],
"result":[np.NaN for _ in range(7)]})
I would like to perform the following operation with cummin() and cummax() on it:
example["result"].apply(lambda x : x= example["value"].cummax() if example["dirr"]==1
else x= example["value"].cummin() if example["dirr"]==-1
else x= NaN if if example["dirr"]==0
)
this is returning : error: invalid syntax
.
Could anyone help me straightening that one up?
That would be the intended output:
example = pd.DataFrame({"dirr":[1,0,-1,-1,1,-1,0],
"value": [125,130,80,8,150,251,18],
"result":[125, NaN, 80, 8, 150, 8, NaN]})
EDIT:
So as per the answer of @su79eu7k the following function would do:
def calc(x):
if x['dirr'] == 1:
return np.diag(example["value"].cummax())
elif x['dirr'] == -1:
return np.diag(example["value"].cummin())
else:
return np.nan
I should be able to shove that into a lambda but still am blocked on the syntax error... which I still don't see?
example["result"]=example.apply(lambda x : np.diag(x["value"].cummax()) if x["dirr"]==1
else np.diag(x["value"].cummin()) if x["dirr"]==-1
else NaN if x["dirr"]==0
)
A final little nudge form you guys would be hugely appreciated.