0

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

python throws in this error for the following code-

previous = uniqueYear[0]
sum = 0

for i in raw['Year']:
    while(raw['Year']==previous):
        sum+=raw['precipMM']
        if(raw['Year']!=previous):
            break
        print(sum)
        previous=raw['Year']
        sum=0

uniqueYear is an array containing all the unique elements in the 'Year' column of my dataframe 'raw'.

I want to add all the values in the column 'precipMM' for the same value in the 'year' column. example- when the 'Year' column says 2009, i want to add all the values in 'precipMM' and print it, then move on to the next value in 'Year' i.e 2010, do the same for that and so on and so on.

  • You're looping over `raw["Year"]`, but you're not using `i` anywhere. Instead you're comparing the full `raw["Year"]` to `previous` every iteration... – ShlomiF Feb 06 '23 at 12:12
  • You likely want: `df['new_col'] = df.groupby('Year')['precipMM'].tranform('sum')`, or a variation of that. – mozway Feb 06 '23 at 12:35

0 Answers0