samedi 4 avril 2020

Ambiguous Truth Value - For Loop & If Statements

Chasing the reasoning behind the following error message.

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

This is the code I am running:

anz_analysis["Action"] = anz_analysis["Signal"]        

for i in range(0, len(anz_analysis) + 1):       
    if ((anz_analysis["Signal"].iloc[[i]] == "Buy") & (anz_analysis["Signal"].iloc[[i+1]] == "Buy")):
        anz_analysis["Action"] = anz_analysis["Signal"].iloc[[i]] = "Maintain"
    elif (anz_analysis["Signal"].iloc[[i]] = "Sell") & (anz_analysis["Signal"].iloc[[i+1]] = "Sell"):
        anz_analysis["Signal"].iloc[[i]] = "Maintain"

The dataframe looks like this:


Current: Wanted: 
_______|_________
1|Buy  | Buy
2|Buy  | Maintain
3|Buy  | Maintain
4|Sell | Sell
5|Sell | Maintain
6|Sell | Maintain

Any help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire