# Comparing dataframe object with string value in django

I'm implementing machine learning model and using training dataset from MySQL table and all this is built upon Django. So basically all the calculations are done by converting entire data from MySQL table to dataframe.

df = pd.read_sql("select * from naivebayes_player",connection)


However, I'm facing problem in comparing dataframe column value with a string.

So I have a column named classification in MySQL table which has 2 fixed values 'RS' or 'NRS' stored in varchar(10) format. Since I've converted an entire table into dataframe whenever I calculate the count of 'RS' values in classification column in dataframe it always returns 0. But actually, there are 63 entries of 'RS'.

total_RS = df['classification'][df['classification']=='RS'].count()


In above line of code I'm trying to find out all records where classification is 'RS' which should be 63 but I'm getting 0. What am I doing wrong?

I have tried above code when reading data from CSV instead of MySQL table and everything worked fine.

df['classification'].value_counts()['RS']

If you omit the indexing at the end there, the call to value_counts() will concretely show you what values appear with what frequency in the classification column, which might help you debug what's going on if this still doesn't work.
• Right, my suggestion in my answer was if that happens, omit the last index to see what's going on. Running df['classification'].value_counts() will show tiki each value in that column and their frequencies, since pandas is telling you RS doesn't actually occur in that column. Jan 13 '18 at 17:09
• It appears in the output of .value_counts()? Maybe there's whitespace in the field value, e.g. rather than 'RS' it's actually 'RS ' Jan 14 '18 at 7:20