0

I'm using this repository:

https://github.com/fivethirtyeight/data/blob/master/avengers/avengers.csv

For an exercise in DataQuest, I have to count the number of 'Years since joining' is correct by subtracting 2015 (reference year) from column 'Year'.

I'm trying to use a for and if loop to do this simple task but I am having a hard time figuring out. How do I incorporate the 'for row' into the loop?

def Years_joined():    
    joined_accuracy_count = 0

    for row in avengers['Years since joining']:
        if (2015 - avengers['Year']) == avengers['Years since joining']:
            joined_accuracy_count += 1
    return joined_accuracy_count

EDIT: Sorry for not giving more information. The file is in pandas. So pd.read_csv('avengers')

I have two columns. 'Year' and 'Years since joining'. For example, Year would be 1963. Years since joining would be 52. I am trying to write a for-if loop to see if 2015 - 1963 = 52. And if so, add that to a count.

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

5
  • You can just reference row in the loop. See the first example here: wiki.python.org/moin/ForLoop Commented Nov 21, 2018 at 19:40
  • You haven't shown how you read in the CSV. DictReader()? pandas? Commented Nov 21, 2018 at 19:40
  • 1
    If avengers['Years since joining'] is just an array of years, then row represents the current selected year for each loop iteration. So you'd just have if (2015 - row) == .... Commented Nov 21, 2018 at 19:41
  • @lurker Years since joining isn't an array of years. It's 2015 minus Years, so a two digit number. I am supposed to write a for if loop to count if this is true for all rows. Commented Nov 21, 2018 at 20:04
  • What I meant was "an array of number of years", i.e., numeric number of years. Commented Nov 21, 2018 at 20:52

1 Answer 1

1

I think you want to use avengers.iterrows().
Basically, you're running over the values of a specific column, but that's not a "row" in the sense you want, and you have no access to other columns this way.
Try -

for _, row in avengers.iterrows():
    if (2015 - row['Year']) == row['Years since joining']:
        joined_accuracy_count += 1
return joined_accuracy_count

P.S: This is only responding to the error you had. If there are other issues with what you're trying to do and how you're going about it that's a different story.

Sign up to request clarification or add additional context in comments.

3 Comments

On a side note, the error you were getting is because avengers['Year'] and its counter-part avengers['Years since joining'] are the full columns.
Thanks! I think this is exactly what I'm looking for.
Well, accepting and/or up-voting helps other users and the community as a whole ;-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.