Counting using if inside for loop












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().










share|improve this question
























  • You can just reference row in the loop. See the first example here: wiki.python.org/moin/ForLoop
    – Robert Harvey
    Nov 21 '18 at 19:40












  • You haven't shown how you read in the CSV. DictReader()? pandas?
    – roganjosh
    Nov 21 '18 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) == ....
    – lurker
    Nov 21 '18 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.
    – Julian
    Nov 21 '18 at 20:04












  • What I meant was "an array of number of years", i.e., numeric number of years.
    – lurker
    Nov 21 '18 at 20:52
















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().










share|improve this question
























  • You can just reference row in the loop. See the first example here: wiki.python.org/moin/ForLoop
    – Robert Harvey
    Nov 21 '18 at 19:40












  • You haven't shown how you read in the CSV. DictReader()? pandas?
    – roganjosh
    Nov 21 '18 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) == ....
    – lurker
    Nov 21 '18 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.
    – Julian
    Nov 21 '18 at 20:04












  • What I meant was "an array of number of years", i.e., numeric number of years.
    – lurker
    Nov 21 '18 at 20:52














0












0








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().










share|improve this question















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().







python for-loop if-statement






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 20:06







Julian

















asked Nov 21 '18 at 19:39









JulianJulian

336




336












  • You can just reference row in the loop. See the first example here: wiki.python.org/moin/ForLoop
    – Robert Harvey
    Nov 21 '18 at 19:40












  • You haven't shown how you read in the CSV. DictReader()? pandas?
    – roganjosh
    Nov 21 '18 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) == ....
    – lurker
    Nov 21 '18 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.
    – Julian
    Nov 21 '18 at 20:04












  • What I meant was "an array of number of years", i.e., numeric number of years.
    – lurker
    Nov 21 '18 at 20:52


















  • You can just reference row in the loop. See the first example here: wiki.python.org/moin/ForLoop
    – Robert Harvey
    Nov 21 '18 at 19:40












  • You haven't shown how you read in the CSV. DictReader()? pandas?
    – roganjosh
    Nov 21 '18 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) == ....
    – lurker
    Nov 21 '18 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.
    – Julian
    Nov 21 '18 at 20:04












  • What I meant was "an array of number of years", i.e., numeric number of years.
    – lurker
    Nov 21 '18 at 20:52
















You can just reference row in the loop. See the first example here: wiki.python.org/moin/ForLoop
– Robert Harvey
Nov 21 '18 at 19:40






You can just reference row in the loop. See the first example here: wiki.python.org/moin/ForLoop
– Robert Harvey
Nov 21 '18 at 19:40














You haven't shown how you read in the CSV. DictReader()? pandas?
– roganjosh
Nov 21 '18 at 19:40






You haven't shown how you read in the CSV. DictReader()? pandas?
– roganjosh
Nov 21 '18 at 19:40






1




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) == ....
– lurker
Nov 21 '18 at 19:41






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) == ....
– lurker
Nov 21 '18 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.
– Julian
Nov 21 '18 at 20:04






@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.
– Julian
Nov 21 '18 at 20:04














What I meant was "an array of number of years", i.e., numeric number of years.
– lurker
Nov 21 '18 at 20:52




What I meant was "an array of number of years", i.e., numeric number of years.
– lurker
Nov 21 '18 at 20:52












1 Answer
1






active

oldest

votes


















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.






share|improve this answer





















  • 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.
    – ShlomiF
    Nov 21 '18 at 20:10






  • 1




    Thanks! I think this is exactly what I'm looking for.
    – Julian
    Nov 21 '18 at 20:29










  • Well, accepting and/or up-voting helps other users and the community as a whole ;-)
    – ShlomiF
    Nov 21 '18 at 20:31











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419412%2fcounting-using-if-inside-for-loop%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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.






share|improve this answer





















  • 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.
    – ShlomiF
    Nov 21 '18 at 20:10






  • 1




    Thanks! I think this is exactly what I'm looking for.
    – Julian
    Nov 21 '18 at 20:29










  • Well, accepting and/or up-voting helps other users and the community as a whole ;-)
    – ShlomiF
    Nov 21 '18 at 20:31
















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.






share|improve this answer





















  • 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.
    – ShlomiF
    Nov 21 '18 at 20:10






  • 1




    Thanks! I think this is exactly what I'm looking for.
    – Julian
    Nov 21 '18 at 20:29










  • Well, accepting and/or up-voting helps other users and the community as a whole ;-)
    – ShlomiF
    Nov 21 '18 at 20:31














1












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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 20:06









ShlomiFShlomiF

832410




832410












  • 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.
    – ShlomiF
    Nov 21 '18 at 20:10






  • 1




    Thanks! I think this is exactly what I'm looking for.
    – Julian
    Nov 21 '18 at 20:29










  • Well, accepting and/or up-voting helps other users and the community as a whole ;-)
    – ShlomiF
    Nov 21 '18 at 20:31


















  • 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.
    – ShlomiF
    Nov 21 '18 at 20:10






  • 1




    Thanks! I think this is exactly what I'm looking for.
    – Julian
    Nov 21 '18 at 20:29










  • Well, accepting and/or up-voting helps other users and the community as a whole ;-)
    – ShlomiF
    Nov 21 '18 at 20:31
















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.
– ShlomiF
Nov 21 '18 at 20:10




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.
– ShlomiF
Nov 21 '18 at 20:10




1




1




Thanks! I think this is exactly what I'm looking for.
– Julian
Nov 21 '18 at 20:29




Thanks! I think this is exactly what I'm looking for.
– Julian
Nov 21 '18 at 20:29












Well, accepting and/or up-voting helps other users and the community as a whole ;-)
– ShlomiF
Nov 21 '18 at 20:31




Well, accepting and/or up-voting helps other users and the community as a whole ;-)
– ShlomiF
Nov 21 '18 at 20:31


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419412%2fcounting-using-if-inside-for-loop%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python