lundi 30 novembre 2020

ValueError: The truth value of a Series is ambiguous. When using for loop over a dataframe

I am a newbie in the world of python. I have a dataframe that holds ETF Fund "Score" values, updated every day for the past ten years. I am trying to group these together, by collecting the day when the score first passes above a certain value, say 4, and then the day before it passes below 4. I get an error that says the truth value of a series is ambiguous. Please let me know what I am doing wrong.

Here is an example of the dataframe:

Date(index)     Score
1/20/2010       1.489
1/21/2010       1.9
1/22/2010       4
1/23/2010       4.333
1/24/2010       4.6
1/25/2010       3.9

Here is the code I wrote:

csv.columns =['Date', 'Score']

csv['Date'] = pd.to_datetime(csv['Date'])

csv = csv.set_index('Date')
csv = csv.sort_index().asfreq('D', method='pad')
csv['Score'] = csv['Score'].astype(int)

for val in csv.Score:
    if csv.Score >= 4:
        if csv.Score.shift(-1) <4 or csv.Score.shift() <4:
            print(val)
        else:
            continue
    else:
        continue

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

The output I would like to achieve is a dataframe consisting of even (including zero) index values being the start date of Score > 4 and the odd index values the end date of Score > 4. Please help.

Aucun commentaire:

Enregistrer un commentaire