1
counter = 0

for i in range(len(df)):
 print[df.loc[i, "Emotional State"]]
 if i in ["Happy"]:
   counter+=1

print(['counter: ', counter]

The print results is: b'Happy' and the data in the dataset is "Happy" no spaces etc. For some reason i is never equal to Happy, which is not possible as it will be the first iteration. Any ideas why the output is not matching the if statement or data in the frame? The counter is always 0

2
  • What is the output of 1 in ['Happy']? ;) Commented Mar 4, 2022 at 13:21
  • @mozway b'Happy' Commented Mar 4, 2022 at 13:26

1 Answer 1

2

You should not iterate rows when you can apply a vectorial function. Also, here your i is an integer so it will never match "Happy".

Use instead for inclusion in a list:

df["Emotional State"].isin(['Happy']).sum()

or, for exact match:

df["Emotional State"].eq('Happy').sum()

or, for partial match:

df["Emotional State"].str.contains('Happy').sum()

or, to count all (exact) values:

df["Emotional State"].value_counts()

S9 dtype

those are bytes, you need to convert to string first:

df['Emotional State'] = df['Emotional State'].str.decode("utf-8")
Sign up to request clarification or add additional context in comments.

4 Comments

df["Emotional State"].isin(['Happy']).sum() is still 0
@DigimanXT then you have no "Happy" value in the column. Please edit your question to provide a sample of the data (as text)
it could be the datatype of that column "S9" maybe
@DigimanXT you have bytes, convert to string with :df['Emotional State'].str.decode("utf-8")

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.