0
$\begingroup$

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.

$\endgroup$
0
$\begingroup$

I don't see anything glaringly wrong with your code, but you can try this instead:

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.

$\endgroup$
  • $\begingroup$ thank you for your response. I have tried above method it throws KeyError at that line. Exception Type: KeyError, Exception Value: 'RS' $\endgroup$ – Scorpionk Jan 13 '18 at 15:18
  • $\begingroup$ 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. $\endgroup$ – David Marx Jan 13 '18 at 17:09
  • $\begingroup$ 'RS' is there in the column. Maybe the problem is with the data type at the time of comparison. Since the same code works with CSV. $\endgroup$ – Scorpionk Jan 14 '18 at 7:12
  • $\begingroup$ 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 ' $\endgroup$ – David Marx Jan 14 '18 at 7:20
  • $\begingroup$ Actually, I'm on busy schedule right now. Will let you know before tonight about .value_counts() result. $\endgroup$ – Scorpionk Jan 16 '18 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.