Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Trying to for-loop over and find specific strings in a dataframe using if-statement

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

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading