Skip to content
Advertisement

Looping through Pandas Dataframe Columns to count values

I have a column with 14000+ rows and there is only two numbers in this column.

for i  in df['Yes/No']:
  
  if(i in "Yes"):
    y_counter += 1

  else:
    n_counter += 1

When I try this I get an equal 12/12 return for each counter. That is definitely not correct. How do I loop through and count the Yes and Nos?

Advertisement

Answer

You can do it like this :

y_counter = (data['Yes/No'] == 'yes').sum()
n_counter = (data['Yes/No'] == 'no').sum()
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement