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

Time:03-04

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

CodePudding user response:

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")

CodePudding user response:

The datatype was :"S9" and this was causing the issue. The code works as intended now.

  • Related