How to write Multiple If-Else with Return Integer value












1















def score():
edu = df.dummy
if edu == 1:
score= 10
elif edu == 2:
score= 30
elif edu == 3:
score= 80
elif edu == 4:
score= 100
elif edu == 5:
score= 125
elif edu == 6:
score= 150
elif edu == 7:
score= 200
else: return score



Error Occured : The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
How to return a integer value with Multiple if else conditions











share|improve this question

























  • What is edu? What is df?

    – Mureinik
    Nov 22 '18 at 7:12











  • what's inside edu?

    – Vikas Gautam
    Nov 22 '18 at 7:13











  • Hi Mureinik, df is a Data Frame with dummy as a column of integer type and I am storing that value in edu(like 1,2,3,4,5,6,7,8)

    – Kranthi Kumar Reddy
    Nov 22 '18 at 7:15













  • You are comparing entire pandas.Series with single int value that is why you are getting above error.

    – Sociopath
    Nov 22 '18 at 7:16











  • You want to return score right?

    – Sandesh34
    Nov 22 '18 at 7:22


















1















def score():
edu = df.dummy
if edu == 1:
score= 10
elif edu == 2:
score= 30
elif edu == 3:
score= 80
elif edu == 4:
score= 100
elif edu == 5:
score= 125
elif edu == 6:
score= 150
elif edu == 7:
score= 200
else: return score



Error Occured : The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
How to return a integer value with Multiple if else conditions











share|improve this question

























  • What is edu? What is df?

    – Mureinik
    Nov 22 '18 at 7:12











  • what's inside edu?

    – Vikas Gautam
    Nov 22 '18 at 7:13











  • Hi Mureinik, df is a Data Frame with dummy as a column of integer type and I am storing that value in edu(like 1,2,3,4,5,6,7,8)

    – Kranthi Kumar Reddy
    Nov 22 '18 at 7:15













  • You are comparing entire pandas.Series with single int value that is why you are getting above error.

    – Sociopath
    Nov 22 '18 at 7:16











  • You want to return score right?

    – Sandesh34
    Nov 22 '18 at 7:22
















1












1








1








def score():
edu = df.dummy
if edu == 1:
score= 10
elif edu == 2:
score= 30
elif edu == 3:
score= 80
elif edu == 4:
score= 100
elif edu == 5:
score= 125
elif edu == 6:
score= 150
elif edu == 7:
score= 200
else: return score



Error Occured : The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
How to return a integer value with Multiple if else conditions











share|improve this question
















def score():
edu = df.dummy
if edu == 1:
score= 10
elif edu == 2:
score= 30
elif edu == 3:
score= 80
elif edu == 4:
score= 100
elif edu == 5:
score= 125
elif edu == 6:
score= 150
elif edu == 7:
score= 200
else: return score



Error Occured : The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
How to return a integer value with Multiple if else conditions








python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 7:23









Sreeram TP

2,68921134




2,68921134










asked Nov 22 '18 at 7:08









Kranthi Kumar ReddyKranthi Kumar Reddy

102




102













  • What is edu? What is df?

    – Mureinik
    Nov 22 '18 at 7:12











  • what's inside edu?

    – Vikas Gautam
    Nov 22 '18 at 7:13











  • Hi Mureinik, df is a Data Frame with dummy as a column of integer type and I am storing that value in edu(like 1,2,3,4,5,6,7,8)

    – Kranthi Kumar Reddy
    Nov 22 '18 at 7:15













  • You are comparing entire pandas.Series with single int value that is why you are getting above error.

    – Sociopath
    Nov 22 '18 at 7:16











  • You want to return score right?

    – Sandesh34
    Nov 22 '18 at 7:22





















  • What is edu? What is df?

    – Mureinik
    Nov 22 '18 at 7:12











  • what's inside edu?

    – Vikas Gautam
    Nov 22 '18 at 7:13











  • Hi Mureinik, df is a Data Frame with dummy as a column of integer type and I am storing that value in edu(like 1,2,3,4,5,6,7,8)

    – Kranthi Kumar Reddy
    Nov 22 '18 at 7:15













  • You are comparing entire pandas.Series with single int value that is why you are getting above error.

    – Sociopath
    Nov 22 '18 at 7:16











  • You want to return score right?

    – Sandesh34
    Nov 22 '18 at 7:22



















What is edu? What is df?

– Mureinik
Nov 22 '18 at 7:12





What is edu? What is df?

– Mureinik
Nov 22 '18 at 7:12













what's inside edu?

– Vikas Gautam
Nov 22 '18 at 7:13





what's inside edu?

– Vikas Gautam
Nov 22 '18 at 7:13













Hi Mureinik, df is a Data Frame with dummy as a column of integer type and I am storing that value in edu(like 1,2,3,4,5,6,7,8)

– Kranthi Kumar Reddy
Nov 22 '18 at 7:15







Hi Mureinik, df is a Data Frame with dummy as a column of integer type and I am storing that value in edu(like 1,2,3,4,5,6,7,8)

– Kranthi Kumar Reddy
Nov 22 '18 at 7:15















You are comparing entire pandas.Series with single int value that is why you are getting above error.

– Sociopath
Nov 22 '18 at 7:16





You are comparing entire pandas.Series with single int value that is why you are getting above error.

– Sociopath
Nov 22 '18 at 7:16













You want to return score right?

– Sandesh34
Nov 22 '18 at 7:22







You want to return score right?

– Sandesh34
Nov 22 '18 at 7:22














4 Answers
4






active

oldest

votes


















0














def edu_score():
edu = df.dummy
edu_score=
for i in edu:
if(i == 1):
score = 10
elif(i == 2):
score = 30
elif(i == 3):
score = 80
elif(i == 4):
score = 100
elif(i == 5):
score = 125
elif(i == 6):
score = 150
elif(i == 7):
score = 200
edu_score.append(score)
return edu_score


Here is your answer






share|improve this answer































    0














    Firstly, if edu is bigger than 7, score is not assigned to any value. So you cannot return score.



    And, you can try df = pd.get_dummies(df, columns=['type']).






    share|improve this answer
























    • edu consists of 9 values from 0 to 8

      – Kranthi Kumar Reddy
      Nov 22 '18 at 7:21











    • The problem is about score. Not edu. If edu is 0 or 8, you dont assign any value to "score" and you try to return score.

      – tgbzkl
      Nov 22 '18 at 7:27



















    0














    def score(edu):
    if edu == 1:
    score= 10
    elif edu == 2:
    score= 30
    elif edu == 3:
    score= 80
    elif edu == 4:
    score= 100
    elif edu == 5:
    score= 125
    elif edu == 6:
    score= 150
    elif edu == 7:
    score= 200
    else:
    score=0
    return score


    now, when you are calling the function, pass the dataframe as
    score(df.dummy).






    share|improve this answer































      0














      You just need to clear the else statement and you're done



      def scores():
      score=0
      edu = df.dummy

      if edu == 1:
      score= 10
      elif edu == 2:
      score= 30
      elif edu == 3:
      score= 80
      elif edu == 4:
      score= 100
      elif edu == 5:
      score= 125
      elif edu == 6:
      score= 150
      elif edu == 7:
      score= 200
      return score





      share|improve this answer


























      • The code u gave works fine but it takes edu == 7 only

        – Kranthi Kumar Reddy
        Nov 22 '18 at 7:48











      • oh! I accidentally put the line edu=7 in the function. Wait, I'll edit.

        – Sandesh34
        Nov 22 '18 at 7:49











      • @KranthiKumarReddy Try now

        – Sandesh34
        Nov 22 '18 at 7:50











      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      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: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      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%2fstackoverflow.com%2fquestions%2f53425552%2fhow-to-write-multiple-if-else-with-return-integer-value%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      def edu_score():
      edu = df.dummy
      edu_score=
      for i in edu:
      if(i == 1):
      score = 10
      elif(i == 2):
      score = 30
      elif(i == 3):
      score = 80
      elif(i == 4):
      score = 100
      elif(i == 5):
      score = 125
      elif(i == 6):
      score = 150
      elif(i == 7):
      score = 200
      edu_score.append(score)
      return edu_score


      Here is your answer






      share|improve this answer




























        0














        def edu_score():
        edu = df.dummy
        edu_score=
        for i in edu:
        if(i == 1):
        score = 10
        elif(i == 2):
        score = 30
        elif(i == 3):
        score = 80
        elif(i == 4):
        score = 100
        elif(i == 5):
        score = 125
        elif(i == 6):
        score = 150
        elif(i == 7):
        score = 200
        edu_score.append(score)
        return edu_score


        Here is your answer






        share|improve this answer


























          0












          0








          0







          def edu_score():
          edu = df.dummy
          edu_score=
          for i in edu:
          if(i == 1):
          score = 10
          elif(i == 2):
          score = 30
          elif(i == 3):
          score = 80
          elif(i == 4):
          score = 100
          elif(i == 5):
          score = 125
          elif(i == 6):
          score = 150
          elif(i == 7):
          score = 200
          edu_score.append(score)
          return edu_score


          Here is your answer






          share|improve this answer













          def edu_score():
          edu = df.dummy
          edu_score=
          for i in edu:
          if(i == 1):
          score = 10
          elif(i == 2):
          score = 30
          elif(i == 3):
          score = 80
          elif(i == 4):
          score = 100
          elif(i == 5):
          score = 125
          elif(i == 6):
          score = 150
          elif(i == 7):
          score = 200
          edu_score.append(score)
          return edu_score


          Here is your answer







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 8:44









          Meghana ReddyMeghana Reddy

          504




          504

























              0














              Firstly, if edu is bigger than 7, score is not assigned to any value. So you cannot return score.



              And, you can try df = pd.get_dummies(df, columns=['type']).






              share|improve this answer
























              • edu consists of 9 values from 0 to 8

                – Kranthi Kumar Reddy
                Nov 22 '18 at 7:21











              • The problem is about score. Not edu. If edu is 0 or 8, you dont assign any value to "score" and you try to return score.

                – tgbzkl
                Nov 22 '18 at 7:27
















              0














              Firstly, if edu is bigger than 7, score is not assigned to any value. So you cannot return score.



              And, you can try df = pd.get_dummies(df, columns=['type']).






              share|improve this answer
























              • edu consists of 9 values from 0 to 8

                – Kranthi Kumar Reddy
                Nov 22 '18 at 7:21











              • The problem is about score. Not edu. If edu is 0 or 8, you dont assign any value to "score" and you try to return score.

                – tgbzkl
                Nov 22 '18 at 7:27














              0












              0








              0







              Firstly, if edu is bigger than 7, score is not assigned to any value. So you cannot return score.



              And, you can try df = pd.get_dummies(df, columns=['type']).






              share|improve this answer













              Firstly, if edu is bigger than 7, score is not assigned to any value. So you cannot return score.



              And, you can try df = pd.get_dummies(df, columns=['type']).







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 22 '18 at 7:18









              tgbzkltgbzkl

              1919




              1919













              • edu consists of 9 values from 0 to 8

                – Kranthi Kumar Reddy
                Nov 22 '18 at 7:21











              • The problem is about score. Not edu. If edu is 0 or 8, you dont assign any value to "score" and you try to return score.

                – tgbzkl
                Nov 22 '18 at 7:27



















              • edu consists of 9 values from 0 to 8

                – Kranthi Kumar Reddy
                Nov 22 '18 at 7:21











              • The problem is about score. Not edu. If edu is 0 or 8, you dont assign any value to "score" and you try to return score.

                – tgbzkl
                Nov 22 '18 at 7:27

















              edu consists of 9 values from 0 to 8

              – Kranthi Kumar Reddy
              Nov 22 '18 at 7:21





              edu consists of 9 values from 0 to 8

              – Kranthi Kumar Reddy
              Nov 22 '18 at 7:21













              The problem is about score. Not edu. If edu is 0 or 8, you dont assign any value to "score" and you try to return score.

              – tgbzkl
              Nov 22 '18 at 7:27





              The problem is about score. Not edu. If edu is 0 or 8, you dont assign any value to "score" and you try to return score.

              – tgbzkl
              Nov 22 '18 at 7:27











              0














              def score(edu):
              if edu == 1:
              score= 10
              elif edu == 2:
              score= 30
              elif edu == 3:
              score= 80
              elif edu == 4:
              score= 100
              elif edu == 5:
              score= 125
              elif edu == 6:
              score= 150
              elif edu == 7:
              score= 200
              else:
              score=0
              return score


              now, when you are calling the function, pass the dataframe as
              score(df.dummy).






              share|improve this answer




























                0














                def score(edu):
                if edu == 1:
                score= 10
                elif edu == 2:
                score= 30
                elif edu == 3:
                score= 80
                elif edu == 4:
                score= 100
                elif edu == 5:
                score= 125
                elif edu == 6:
                score= 150
                elif edu == 7:
                score= 200
                else:
                score=0
                return score


                now, when you are calling the function, pass the dataframe as
                score(df.dummy).






                share|improve this answer


























                  0












                  0








                  0







                  def score(edu):
                  if edu == 1:
                  score= 10
                  elif edu == 2:
                  score= 30
                  elif edu == 3:
                  score= 80
                  elif edu == 4:
                  score= 100
                  elif edu == 5:
                  score= 125
                  elif edu == 6:
                  score= 150
                  elif edu == 7:
                  score= 200
                  else:
                  score=0
                  return score


                  now, when you are calling the function, pass the dataframe as
                  score(df.dummy).






                  share|improve this answer













                  def score(edu):
                  if edu == 1:
                  score= 10
                  elif edu == 2:
                  score= 30
                  elif edu == 3:
                  score= 80
                  elif edu == 4:
                  score= 100
                  elif edu == 5:
                  score= 125
                  elif edu == 6:
                  score= 150
                  elif edu == 7:
                  score= 200
                  else:
                  score=0
                  return score


                  now, when you are calling the function, pass the dataframe as
                  score(df.dummy).







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 '18 at 7:32









                  Meghana ReddyMeghana Reddy

                  504




                  504























                      0














                      You just need to clear the else statement and you're done



                      def scores():
                      score=0
                      edu = df.dummy

                      if edu == 1:
                      score= 10
                      elif edu == 2:
                      score= 30
                      elif edu == 3:
                      score= 80
                      elif edu == 4:
                      score= 100
                      elif edu == 5:
                      score= 125
                      elif edu == 6:
                      score= 150
                      elif edu == 7:
                      score= 200
                      return score





                      share|improve this answer


























                      • The code u gave works fine but it takes edu == 7 only

                        – Kranthi Kumar Reddy
                        Nov 22 '18 at 7:48











                      • oh! I accidentally put the line edu=7 in the function. Wait, I'll edit.

                        – Sandesh34
                        Nov 22 '18 at 7:49











                      • @KranthiKumarReddy Try now

                        – Sandesh34
                        Nov 22 '18 at 7:50
















                      0














                      You just need to clear the else statement and you're done



                      def scores():
                      score=0
                      edu = df.dummy

                      if edu == 1:
                      score= 10
                      elif edu == 2:
                      score= 30
                      elif edu == 3:
                      score= 80
                      elif edu == 4:
                      score= 100
                      elif edu == 5:
                      score= 125
                      elif edu == 6:
                      score= 150
                      elif edu == 7:
                      score= 200
                      return score





                      share|improve this answer


























                      • The code u gave works fine but it takes edu == 7 only

                        – Kranthi Kumar Reddy
                        Nov 22 '18 at 7:48











                      • oh! I accidentally put the line edu=7 in the function. Wait, I'll edit.

                        – Sandesh34
                        Nov 22 '18 at 7:49











                      • @KranthiKumarReddy Try now

                        – Sandesh34
                        Nov 22 '18 at 7:50














                      0












                      0








                      0







                      You just need to clear the else statement and you're done



                      def scores():
                      score=0
                      edu = df.dummy

                      if edu == 1:
                      score= 10
                      elif edu == 2:
                      score= 30
                      elif edu == 3:
                      score= 80
                      elif edu == 4:
                      score= 100
                      elif edu == 5:
                      score= 125
                      elif edu == 6:
                      score= 150
                      elif edu == 7:
                      score= 200
                      return score





                      share|improve this answer















                      You just need to clear the else statement and you're done



                      def scores():
                      score=0
                      edu = df.dummy

                      if edu == 1:
                      score= 10
                      elif edu == 2:
                      score= 30
                      elif edu == 3:
                      score= 80
                      elif edu == 4:
                      score= 100
                      elif edu == 5:
                      score= 125
                      elif edu == 6:
                      score= 150
                      elif edu == 7:
                      score= 200
                      return score






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 22 '18 at 7:49

























                      answered Nov 22 '18 at 7:27









                      Sandesh34Sandesh34

                      254112




                      254112













                      • The code u gave works fine but it takes edu == 7 only

                        – Kranthi Kumar Reddy
                        Nov 22 '18 at 7:48











                      • oh! I accidentally put the line edu=7 in the function. Wait, I'll edit.

                        – Sandesh34
                        Nov 22 '18 at 7:49











                      • @KranthiKumarReddy Try now

                        – Sandesh34
                        Nov 22 '18 at 7:50



















                      • The code u gave works fine but it takes edu == 7 only

                        – Kranthi Kumar Reddy
                        Nov 22 '18 at 7:48











                      • oh! I accidentally put the line edu=7 in the function. Wait, I'll edit.

                        – Sandesh34
                        Nov 22 '18 at 7:49











                      • @KranthiKumarReddy Try now

                        – Sandesh34
                        Nov 22 '18 at 7:50

















                      The code u gave works fine but it takes edu == 7 only

                      – Kranthi Kumar Reddy
                      Nov 22 '18 at 7:48





                      The code u gave works fine but it takes edu == 7 only

                      – Kranthi Kumar Reddy
                      Nov 22 '18 at 7:48













                      oh! I accidentally put the line edu=7 in the function. Wait, I'll edit.

                      – Sandesh34
                      Nov 22 '18 at 7:49





                      oh! I accidentally put the line edu=7 in the function. Wait, I'll edit.

                      – Sandesh34
                      Nov 22 '18 at 7:49













                      @KranthiKumarReddy Try now

                      – Sandesh34
                      Nov 22 '18 at 7:50





                      @KranthiKumarReddy Try now

                      – Sandesh34
                      Nov 22 '18 at 7:50


















                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


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


                      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%2fstackoverflow.com%2fquestions%2f53425552%2fhow-to-write-multiple-if-else-with-return-integer-value%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

                      404 Error Contact Form 7 ajax form submitting

                      How to know if a Active Directory user can login interactively

                      TypeError: fit_transform() missing 1 required positional argument: 'X'