How to perform mathematical operations on all CSV file columns & rows using Pandas












2














Here is my code:



all_data = pd.read_csv('data2.csv')
mu = np.array([all_data.mean(0)])
sigma = np.array([np.std(all_data,axis=0)])
print(all_data.shape)
print(mu.shape)
print(sigma.shape)



Output:



(20, 24)



(1, 24)



(1, 24)




Sigma and Mu are numpy array or matrix.



I want to perform this operaiton:




all_data = (all_data - mu)/sigma




Here, first column (all rows), of all_data first gets substracted by first column of mu and then divided by first column of sigma



second column (all rows) of all_data first gets substracted by second column of mu and then divided by second column of sigma



Like that










share|improve this question



























    2














    Here is my code:



    all_data = pd.read_csv('data2.csv')
    mu = np.array([all_data.mean(0)])
    sigma = np.array([np.std(all_data,axis=0)])
    print(all_data.shape)
    print(mu.shape)
    print(sigma.shape)



    Output:



    (20, 24)



    (1, 24)



    (1, 24)




    Sigma and Mu are numpy array or matrix.



    I want to perform this operaiton:




    all_data = (all_data - mu)/sigma




    Here, first column (all rows), of all_data first gets substracted by first column of mu and then divided by first column of sigma



    second column (all rows) of all_data first gets substracted by second column of mu and then divided by second column of sigma



    Like that










    share|improve this question

























      2












      2








      2







      Here is my code:



      all_data = pd.read_csv('data2.csv')
      mu = np.array([all_data.mean(0)])
      sigma = np.array([np.std(all_data,axis=0)])
      print(all_data.shape)
      print(mu.shape)
      print(sigma.shape)



      Output:



      (20, 24)



      (1, 24)



      (1, 24)




      Sigma and Mu are numpy array or matrix.



      I want to perform this operaiton:




      all_data = (all_data - mu)/sigma




      Here, first column (all rows), of all_data first gets substracted by first column of mu and then divided by first column of sigma



      second column (all rows) of all_data first gets substracted by second column of mu and then divided by second column of sigma



      Like that










      share|improve this question













      Here is my code:



      all_data = pd.read_csv('data2.csv')
      mu = np.array([all_data.mean(0)])
      sigma = np.array([np.std(all_data,axis=0)])
      print(all_data.shape)
      print(mu.shape)
      print(sigma.shape)



      Output:



      (20, 24)



      (1, 24)



      (1, 24)




      Sigma and Mu are numpy array or matrix.



      I want to perform this operaiton:




      all_data = (all_data - mu)/sigma




      Here, first column (all rows), of all_data first gets substracted by first column of mu and then divided by first column of sigma



      second column (all rows) of all_data first gets substracted by second column of mu and then divided by second column of sigma



      Like that







      python pandas numpy






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 19 '18 at 15:59









      john doe

      203




      203
























          2 Answers
          2






          active

          oldest

          votes


















          1














          If you work with the underlying numpy arrays of your dataframe, the broadcasting takes care of the work for you:



          (all_data.values - mu)/sigma


          And put it back in a dataframe with the same columns/index as all_data:



          pd.DataFrame((all_data.values - mu)/sigma, columns=all_data.columns, index=all_data.index)


          Example:



          On this mini dataframe:



          all_data = pd.DataFrame(np.random.randint(0,9,(5,5)))
          >>> all_data
          0 1 2 3 4
          0 5 7 1 8 6
          1 5 8 0 3 0
          2 8 2 0 1 6
          3 5 8 7 7 0
          4 4 6 0 2 5


          With:



          mu = np.array([all_data.mean(0)])
          sigma = np.array([np.std(all_data,axis=0)])

          >>> mu
          array([[5.6, 2. , 4. , 4.4, 7.6]])
          >>> sigma
          array([[1.62480768, 1.26491106, 3.40587727, 2.41660919, 0.48989795]])


          You can get:



          >>> pd.DataFrame((all_data.values - mu)/sigma, columns=all_data.columns, index=all_data.index)
          0 1 2 3 4
          0 -0.369274 3.952847 -0.88083 1.489691 -3.265986
          1 -0.369274 4.743416 -1.17444 -0.579324 -15.513435
          2 1.477098 0.000000 -1.17444 -1.406930 -3.265986
          3 -0.369274 4.743416 0.88083 1.075888 -15.513435
          4 -0.984732 3.162278 -1.17444 -0.993127 -5.307228


          Feel free the check the math, but it satisfies your requirements: the operation is applied on the first column of the dataframe with the first values of sigma and mu, second column with second values, etc...






          share|improve this answer





























            0














            how about numpy.matlib.repmat?



            df = pd.DataFrame(numpy.random.rand(20, 24))
            mu = np.array([all_data.mean(0)])
            sigma = np.array([np.std(all_data,axis=0)])

            MU = pd.DataFrame(numpy.matlib.repmat(mu,20, 1))
            SIGMA = pd.DataFrame(numpy.matlib.repmat(sigma,20, 1))
            all_data = (all_data - MU)/SIGMA





            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%2f53378420%2fhow-to-perform-mathematical-operations-on-all-csv-file-columns-rows-using-pand%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









              1














              If you work with the underlying numpy arrays of your dataframe, the broadcasting takes care of the work for you:



              (all_data.values - mu)/sigma


              And put it back in a dataframe with the same columns/index as all_data:



              pd.DataFrame((all_data.values - mu)/sigma, columns=all_data.columns, index=all_data.index)


              Example:



              On this mini dataframe:



              all_data = pd.DataFrame(np.random.randint(0,9,(5,5)))
              >>> all_data
              0 1 2 3 4
              0 5 7 1 8 6
              1 5 8 0 3 0
              2 8 2 0 1 6
              3 5 8 7 7 0
              4 4 6 0 2 5


              With:



              mu = np.array([all_data.mean(0)])
              sigma = np.array([np.std(all_data,axis=0)])

              >>> mu
              array([[5.6, 2. , 4. , 4.4, 7.6]])
              >>> sigma
              array([[1.62480768, 1.26491106, 3.40587727, 2.41660919, 0.48989795]])


              You can get:



              >>> pd.DataFrame((all_data.values - mu)/sigma, columns=all_data.columns, index=all_data.index)
              0 1 2 3 4
              0 -0.369274 3.952847 -0.88083 1.489691 -3.265986
              1 -0.369274 4.743416 -1.17444 -0.579324 -15.513435
              2 1.477098 0.000000 -1.17444 -1.406930 -3.265986
              3 -0.369274 4.743416 0.88083 1.075888 -15.513435
              4 -0.984732 3.162278 -1.17444 -0.993127 -5.307228


              Feel free the check the math, but it satisfies your requirements: the operation is applied on the first column of the dataframe with the first values of sigma and mu, second column with second values, etc...






              share|improve this answer


























                1














                If you work with the underlying numpy arrays of your dataframe, the broadcasting takes care of the work for you:



                (all_data.values - mu)/sigma


                And put it back in a dataframe with the same columns/index as all_data:



                pd.DataFrame((all_data.values - mu)/sigma, columns=all_data.columns, index=all_data.index)


                Example:



                On this mini dataframe:



                all_data = pd.DataFrame(np.random.randint(0,9,(5,5)))
                >>> all_data
                0 1 2 3 4
                0 5 7 1 8 6
                1 5 8 0 3 0
                2 8 2 0 1 6
                3 5 8 7 7 0
                4 4 6 0 2 5


                With:



                mu = np.array([all_data.mean(0)])
                sigma = np.array([np.std(all_data,axis=0)])

                >>> mu
                array([[5.6, 2. , 4. , 4.4, 7.6]])
                >>> sigma
                array([[1.62480768, 1.26491106, 3.40587727, 2.41660919, 0.48989795]])


                You can get:



                >>> pd.DataFrame((all_data.values - mu)/sigma, columns=all_data.columns, index=all_data.index)
                0 1 2 3 4
                0 -0.369274 3.952847 -0.88083 1.489691 -3.265986
                1 -0.369274 4.743416 -1.17444 -0.579324 -15.513435
                2 1.477098 0.000000 -1.17444 -1.406930 -3.265986
                3 -0.369274 4.743416 0.88083 1.075888 -15.513435
                4 -0.984732 3.162278 -1.17444 -0.993127 -5.307228


                Feel free the check the math, but it satisfies your requirements: the operation is applied on the first column of the dataframe with the first values of sigma and mu, second column with second values, etc...






                share|improve this answer
























                  1












                  1








                  1






                  If you work with the underlying numpy arrays of your dataframe, the broadcasting takes care of the work for you:



                  (all_data.values - mu)/sigma


                  And put it back in a dataframe with the same columns/index as all_data:



                  pd.DataFrame((all_data.values - mu)/sigma, columns=all_data.columns, index=all_data.index)


                  Example:



                  On this mini dataframe:



                  all_data = pd.DataFrame(np.random.randint(0,9,(5,5)))
                  >>> all_data
                  0 1 2 3 4
                  0 5 7 1 8 6
                  1 5 8 0 3 0
                  2 8 2 0 1 6
                  3 5 8 7 7 0
                  4 4 6 0 2 5


                  With:



                  mu = np.array([all_data.mean(0)])
                  sigma = np.array([np.std(all_data,axis=0)])

                  >>> mu
                  array([[5.6, 2. , 4. , 4.4, 7.6]])
                  >>> sigma
                  array([[1.62480768, 1.26491106, 3.40587727, 2.41660919, 0.48989795]])


                  You can get:



                  >>> pd.DataFrame((all_data.values - mu)/sigma, columns=all_data.columns, index=all_data.index)
                  0 1 2 3 4
                  0 -0.369274 3.952847 -0.88083 1.489691 -3.265986
                  1 -0.369274 4.743416 -1.17444 -0.579324 -15.513435
                  2 1.477098 0.000000 -1.17444 -1.406930 -3.265986
                  3 -0.369274 4.743416 0.88083 1.075888 -15.513435
                  4 -0.984732 3.162278 -1.17444 -0.993127 -5.307228


                  Feel free the check the math, but it satisfies your requirements: the operation is applied on the first column of the dataframe with the first values of sigma and mu, second column with second values, etc...






                  share|improve this answer












                  If you work with the underlying numpy arrays of your dataframe, the broadcasting takes care of the work for you:



                  (all_data.values - mu)/sigma


                  And put it back in a dataframe with the same columns/index as all_data:



                  pd.DataFrame((all_data.values - mu)/sigma, columns=all_data.columns, index=all_data.index)


                  Example:



                  On this mini dataframe:



                  all_data = pd.DataFrame(np.random.randint(0,9,(5,5)))
                  >>> all_data
                  0 1 2 3 4
                  0 5 7 1 8 6
                  1 5 8 0 3 0
                  2 8 2 0 1 6
                  3 5 8 7 7 0
                  4 4 6 0 2 5


                  With:



                  mu = np.array([all_data.mean(0)])
                  sigma = np.array([np.std(all_data,axis=0)])

                  >>> mu
                  array([[5.6, 2. , 4. , 4.4, 7.6]])
                  >>> sigma
                  array([[1.62480768, 1.26491106, 3.40587727, 2.41660919, 0.48989795]])


                  You can get:



                  >>> pd.DataFrame((all_data.values - mu)/sigma, columns=all_data.columns, index=all_data.index)
                  0 1 2 3 4
                  0 -0.369274 3.952847 -0.88083 1.489691 -3.265986
                  1 -0.369274 4.743416 -1.17444 -0.579324 -15.513435
                  2 1.477098 0.000000 -1.17444 -1.406930 -3.265986
                  3 -0.369274 4.743416 0.88083 1.075888 -15.513435
                  4 -0.984732 3.162278 -1.17444 -0.993127 -5.307228


                  Feel free the check the math, but it satisfies your requirements: the operation is applied on the first column of the dataframe with the first values of sigma and mu, second column with second values, etc...







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 19 '18 at 16:29









                  sacul

                  29.9k41740




                  29.9k41740

























                      0














                      how about numpy.matlib.repmat?



                      df = pd.DataFrame(numpy.random.rand(20, 24))
                      mu = np.array([all_data.mean(0)])
                      sigma = np.array([np.std(all_data,axis=0)])

                      MU = pd.DataFrame(numpy.matlib.repmat(mu,20, 1))
                      SIGMA = pd.DataFrame(numpy.matlib.repmat(sigma,20, 1))
                      all_data = (all_data - MU)/SIGMA





                      share|improve this answer


























                        0














                        how about numpy.matlib.repmat?



                        df = pd.DataFrame(numpy.random.rand(20, 24))
                        mu = np.array([all_data.mean(0)])
                        sigma = np.array([np.std(all_data,axis=0)])

                        MU = pd.DataFrame(numpy.matlib.repmat(mu,20, 1))
                        SIGMA = pd.DataFrame(numpy.matlib.repmat(sigma,20, 1))
                        all_data = (all_data - MU)/SIGMA





                        share|improve this answer
























                          0












                          0








                          0






                          how about numpy.matlib.repmat?



                          df = pd.DataFrame(numpy.random.rand(20, 24))
                          mu = np.array([all_data.mean(0)])
                          sigma = np.array([np.std(all_data,axis=0)])

                          MU = pd.DataFrame(numpy.matlib.repmat(mu,20, 1))
                          SIGMA = pd.DataFrame(numpy.matlib.repmat(sigma,20, 1))
                          all_data = (all_data - MU)/SIGMA





                          share|improve this answer












                          how about numpy.matlib.repmat?



                          df = pd.DataFrame(numpy.random.rand(20, 24))
                          mu = np.array([all_data.mean(0)])
                          sigma = np.array([np.std(all_data,axis=0)])

                          MU = pd.DataFrame(numpy.matlib.repmat(mu,20, 1))
                          SIGMA = pd.DataFrame(numpy.matlib.repmat(sigma,20, 1))
                          all_data = (all_data - MU)/SIGMA






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 19 '18 at 16:35









                          Robert

                          33429




                          33429






























                              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.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • 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%2f53378420%2fhow-to-perform-mathematical-operations-on-all-csv-file-columns-rows-using-pand%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

                              MongoDB - Not Authorized To Execute Command

                              How to fix TextFormField cause rebuild widget in Flutter

                              in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith