How do I multiply certain columns by a constant?












0















I been trying to work on this problem for the past 1 hour but with little to no success and it is a mess.



I have a df



 Age   Bronx   Manhatten  Brooklyn   Queens  
0 10 11 13 12
1 8 7 9 9
2 5 8 7 6
3 3 2 3 4
4 8 6 8 7
5 10 12 13 14
6 11 13 12 10
7 7 8 6 8


How do I multiply all the columns (Bronx, Manhatten, Brooklyn, Queens) for the



age 0 by 0.05
age 1 by 0.02
age 2,3,4, by 0.2


and then for all the other columns drop it.










share|improve this question



























    0















    I been trying to work on this problem for the past 1 hour but with little to no success and it is a mess.



    I have a df



     Age   Bronx   Manhatten  Brooklyn   Queens  
    0 10 11 13 12
    1 8 7 9 9
    2 5 8 7 6
    3 3 2 3 4
    4 8 6 8 7
    5 10 12 13 14
    6 11 13 12 10
    7 7 8 6 8


    How do I multiply all the columns (Bronx, Manhatten, Brooklyn, Queens) for the



    age 0 by 0.05
    age 1 by 0.02
    age 2,3,4, by 0.2


    and then for all the other columns drop it.










    share|improve this question

























      0












      0








      0








      I been trying to work on this problem for the past 1 hour but with little to no success and it is a mess.



      I have a df



       Age   Bronx   Manhatten  Brooklyn   Queens  
      0 10 11 13 12
      1 8 7 9 9
      2 5 8 7 6
      3 3 2 3 4
      4 8 6 8 7
      5 10 12 13 14
      6 11 13 12 10
      7 7 8 6 8


      How do I multiply all the columns (Bronx, Manhatten, Brooklyn, Queens) for the



      age 0 by 0.05
      age 1 by 0.02
      age 2,3,4, by 0.2


      and then for all the other columns drop it.










      share|improve this question














      I been trying to work on this problem for the past 1 hour but with little to no success and it is a mess.



      I have a df



       Age   Bronx   Manhatten  Brooklyn   Queens  
      0 10 11 13 12
      1 8 7 9 9
      2 5 8 7 6
      3 3 2 3 4
      4 8 6 8 7
      5 10 12 13 14
      6 11 13 12 10
      7 7 8 6 8


      How do I multiply all the columns (Bronx, Manhatten, Brooklyn, Queens) for the



      age 0 by 0.05
      age 1 by 0.02
      age 2,3,4, by 0.2


      and then for all the other columns drop it.







      python pandas numpy






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 18:38









      OptimusPrimeOptimusPrime

      18810




      18810
























          2 Answers
          2






          active

          oldest

          votes


















          2














          Using mul with your own dict to map with age



          age=pd.Series({0:0.5,1:0.2,2:0.2,3:0.2,4:0.2})
          df.set_index('Age').mul(age,axis=0).fillna(df.set_index('Age')).reset_index()
          Out[116]:
          index Bronx Manhatten Brooklyn Queens
          0 0 5.0 5.5 6.5 6.0
          1 1 1.6 1.4 1.8 1.8
          2 2 1.0 1.6 1.4 1.2
          3 3 0.6 0.4 0.6 0.8
          4 4 1.6 1.2 1.6 1.4
          5 5 10.0 12.0 13.0 14.0
          6 6 11.0 13.0 12.0 10.0
          7 7 7.0 8.0 6.0 8.0





          share|improve this answer































            1














            There are a lot of ways to do this:



            def multiply_age(age):
            if age == 0:
            age *= 0.05
            elif age == 1:
            age *= 0.02
            elif age in {2, 3, 4}:
            age *= 0.2
            return age

            df['Age'].apply(multiply_age)


            df['Age'].apply(multiply_age) will return a series of age. You can then make it a dataframe by doing pd.DataFrame(df['Age'].apply(multiply_age)).



            You can also try this:



            def multiply_age(row):
            age = row['Age']
            if age == 0:
            age *= 0.05
            elif age == 1:
            age *= 0.02
            elif age in {2, 3, 4}:
            age *= 0.2
            row['Age'] = age
            return row

            df.apply(lambda row: multiply_age(row), axis=1)['Age']





            share|improve this answer























              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%2f53325944%2fhow-do-i-multiply-certain-columns-by-a-constant%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              Using mul with your own dict to map with age



              age=pd.Series({0:0.5,1:0.2,2:0.2,3:0.2,4:0.2})
              df.set_index('Age').mul(age,axis=0).fillna(df.set_index('Age')).reset_index()
              Out[116]:
              index Bronx Manhatten Brooklyn Queens
              0 0 5.0 5.5 6.5 6.0
              1 1 1.6 1.4 1.8 1.8
              2 2 1.0 1.6 1.4 1.2
              3 3 0.6 0.4 0.6 0.8
              4 4 1.6 1.2 1.6 1.4
              5 5 10.0 12.0 13.0 14.0
              6 6 11.0 13.0 12.0 10.0
              7 7 7.0 8.0 6.0 8.0





              share|improve this answer




























                2














                Using mul with your own dict to map with age



                age=pd.Series({0:0.5,1:0.2,2:0.2,3:0.2,4:0.2})
                df.set_index('Age').mul(age,axis=0).fillna(df.set_index('Age')).reset_index()
                Out[116]:
                index Bronx Manhatten Brooklyn Queens
                0 0 5.0 5.5 6.5 6.0
                1 1 1.6 1.4 1.8 1.8
                2 2 1.0 1.6 1.4 1.2
                3 3 0.6 0.4 0.6 0.8
                4 4 1.6 1.2 1.6 1.4
                5 5 10.0 12.0 13.0 14.0
                6 6 11.0 13.0 12.0 10.0
                7 7 7.0 8.0 6.0 8.0





                share|improve this answer


























                  2












                  2








                  2







                  Using mul with your own dict to map with age



                  age=pd.Series({0:0.5,1:0.2,2:0.2,3:0.2,4:0.2})
                  df.set_index('Age').mul(age,axis=0).fillna(df.set_index('Age')).reset_index()
                  Out[116]:
                  index Bronx Manhatten Brooklyn Queens
                  0 0 5.0 5.5 6.5 6.0
                  1 1 1.6 1.4 1.8 1.8
                  2 2 1.0 1.6 1.4 1.2
                  3 3 0.6 0.4 0.6 0.8
                  4 4 1.6 1.2 1.6 1.4
                  5 5 10.0 12.0 13.0 14.0
                  6 6 11.0 13.0 12.0 10.0
                  7 7 7.0 8.0 6.0 8.0





                  share|improve this answer













                  Using mul with your own dict to map with age



                  age=pd.Series({0:0.5,1:0.2,2:0.2,3:0.2,4:0.2})
                  df.set_index('Age').mul(age,axis=0).fillna(df.set_index('Age')).reset_index()
                  Out[116]:
                  index Bronx Manhatten Brooklyn Queens
                  0 0 5.0 5.5 6.5 6.0
                  1 1 1.6 1.4 1.8 1.8
                  2 2 1.0 1.6 1.4 1.2
                  3 3 0.6 0.4 0.6 0.8
                  4 4 1.6 1.2 1.6 1.4
                  5 5 10.0 12.0 13.0 14.0
                  6 6 11.0 13.0 12.0 10.0
                  7 7 7.0 8.0 6.0 8.0






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 18:41









                  Wen-BenWen-Ben

                  117k83469




                  117k83469

























                      1














                      There are a lot of ways to do this:



                      def multiply_age(age):
                      if age == 0:
                      age *= 0.05
                      elif age == 1:
                      age *= 0.02
                      elif age in {2, 3, 4}:
                      age *= 0.2
                      return age

                      df['Age'].apply(multiply_age)


                      df['Age'].apply(multiply_age) will return a series of age. You can then make it a dataframe by doing pd.DataFrame(df['Age'].apply(multiply_age)).



                      You can also try this:



                      def multiply_age(row):
                      age = row['Age']
                      if age == 0:
                      age *= 0.05
                      elif age == 1:
                      age *= 0.02
                      elif age in {2, 3, 4}:
                      age *= 0.2
                      row['Age'] = age
                      return row

                      df.apply(lambda row: multiply_age(row), axis=1)['Age']





                      share|improve this answer




























                        1














                        There are a lot of ways to do this:



                        def multiply_age(age):
                        if age == 0:
                        age *= 0.05
                        elif age == 1:
                        age *= 0.02
                        elif age in {2, 3, 4}:
                        age *= 0.2
                        return age

                        df['Age'].apply(multiply_age)


                        df['Age'].apply(multiply_age) will return a series of age. You can then make it a dataframe by doing pd.DataFrame(df['Age'].apply(multiply_age)).



                        You can also try this:



                        def multiply_age(row):
                        age = row['Age']
                        if age == 0:
                        age *= 0.05
                        elif age == 1:
                        age *= 0.02
                        elif age in {2, 3, 4}:
                        age *= 0.2
                        row['Age'] = age
                        return row

                        df.apply(lambda row: multiply_age(row), axis=1)['Age']





                        share|improve this answer


























                          1












                          1








                          1







                          There are a lot of ways to do this:



                          def multiply_age(age):
                          if age == 0:
                          age *= 0.05
                          elif age == 1:
                          age *= 0.02
                          elif age in {2, 3, 4}:
                          age *= 0.2
                          return age

                          df['Age'].apply(multiply_age)


                          df['Age'].apply(multiply_age) will return a series of age. You can then make it a dataframe by doing pd.DataFrame(df['Age'].apply(multiply_age)).



                          You can also try this:



                          def multiply_age(row):
                          age = row['Age']
                          if age == 0:
                          age *= 0.05
                          elif age == 1:
                          age *= 0.02
                          elif age in {2, 3, 4}:
                          age *= 0.2
                          row['Age'] = age
                          return row

                          df.apply(lambda row: multiply_age(row), axis=1)['Age']





                          share|improve this answer













                          There are a lot of ways to do this:



                          def multiply_age(age):
                          if age == 0:
                          age *= 0.05
                          elif age == 1:
                          age *= 0.02
                          elif age in {2, 3, 4}:
                          age *= 0.2
                          return age

                          df['Age'].apply(multiply_age)


                          df['Age'].apply(multiply_age) will return a series of age. You can then make it a dataframe by doing pd.DataFrame(df['Age'].apply(multiply_age)).



                          You can also try this:



                          def multiply_age(row):
                          age = row['Age']
                          if age == 0:
                          age *= 0.05
                          elif age == 1:
                          age *= 0.02
                          elif age in {2, 3, 4}:
                          age *= 0.2
                          row['Age'] = age
                          return row

                          df.apply(lambda row: multiply_age(row), axis=1)['Age']






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 15 '18 at 18:42









                          Eric WangEric Wang

                          33519




                          33519






























                              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%2f53325944%2fhow-do-i-multiply-certain-columns-by-a-constant%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

                              Bressuire

                              Vorschmack

                              Quarantine