Comparing dataframe object with string value in django












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.










share|improve this question









$endgroup$




bumped to the homepage by Community 9 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.




















    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.










    share|improve this question









    $endgroup$




    bumped to the homepage by Community 9 hours ago


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.


















      0












      0








      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.










      share|improve this question









      $endgroup$




      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.







      machine-learning pandas dataframe






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 13 '18 at 8:19









      ScorpionkScorpionk

      498




      498





      bumped to the homepage by Community 9 hours ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community 9 hours ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
























          1 Answer
          1






          active

          oldest

          votes


















          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.






          share|improve this answer









          $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





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "557"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f26587%2fcomparing-dataframe-object-with-string-value-in-django%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          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.






          share|improve this answer









          $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
















          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.






          share|improve this answer









          $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














          0












          0








          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.






          share|improve this answer









          $endgroup$



          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 13 '18 at 8:40









          David MarxDavid Marx

          2,202412




          2,202412












          • $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


















          • $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
















          $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$
          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$
          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$
          '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$
          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




          $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


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Data Science Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f26587%2fcomparing-dataframe-object-with-string-value-in-django%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Ponta tanko

          Tantalo (mitologio)

          Erzsébet Schaár