TransWikia.com

Comparing dataframe object with string value in django

Data Science Asked by Scorpionk on December 8, 2020

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.

One Answer

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.

Answered by David Marx on December 8, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP