Write python simulation output to a matrix










1















I am trying to take the sum of four columns in a pandas dataframe (which are determined by a random number) and simulate this process 1000 times. I want this to give me 1000 rows each with different results, for each column.



I essentially want to say something like the following:



for i in range(1000):
np.sum(df['A']) = iterations[i, j]


where df['A'] is one of the columns I want to sum for each iteration. That is, 'for each iteration, sum the column values and 'place' this result in a new dataframe called 'iterations', specifying where the result is going to go'. I understand the code doesn't make sense but it describes what I am trying to achieve. To be clear, I do not want to write the result to a csv or txt file.



Thank you in advance for your advice.










share|improve this question


























    1















    I am trying to take the sum of four columns in a pandas dataframe (which are determined by a random number) and simulate this process 1000 times. I want this to give me 1000 rows each with different results, for each column.



    I essentially want to say something like the following:



    for i in range(1000):
    np.sum(df['A']) = iterations[i, j]


    where df['A'] is one of the columns I want to sum for each iteration. That is, 'for each iteration, sum the column values and 'place' this result in a new dataframe called 'iterations', specifying where the result is going to go'. I understand the code doesn't make sense but it describes what I am trying to achieve. To be clear, I do not want to write the result to a csv or txt file.



    Thank you in advance for your advice.










    share|improve this question
























      1












      1








      1








      I am trying to take the sum of four columns in a pandas dataframe (which are determined by a random number) and simulate this process 1000 times. I want this to give me 1000 rows each with different results, for each column.



      I essentially want to say something like the following:



      for i in range(1000):
      np.sum(df['A']) = iterations[i, j]


      where df['A'] is one of the columns I want to sum for each iteration. That is, 'for each iteration, sum the column values and 'place' this result in a new dataframe called 'iterations', specifying where the result is going to go'. I understand the code doesn't make sense but it describes what I am trying to achieve. To be clear, I do not want to write the result to a csv or txt file.



      Thank you in advance for your advice.










      share|improve this question














      I am trying to take the sum of four columns in a pandas dataframe (which are determined by a random number) and simulate this process 1000 times. I want this to give me 1000 rows each with different results, for each column.



      I essentially want to say something like the following:



      for i in range(1000):
      np.sum(df['A']) = iterations[i, j]


      where df['A'] is one of the columns I want to sum for each iteration. That is, 'for each iteration, sum the column values and 'place' this result in a new dataframe called 'iterations', specifying where the result is going to go'. I understand the code doesn't make sense but it describes what I am trying to achieve. To be clear, I do not want to write the result to a csv or txt file.



      Thank you in advance for your advice.







      pandas numpy






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 17:37









      EarlofMarEarlofMar

      276




      276






















          2 Answers
          2






          active

          oldest

          votes


















          1














          Take the sum of four columns in a pandas dataframe (which are determined by a random number) and simulate this process 1000 times. This should give me 1000 rows each with different results, for each column. We can write:



          import os
          import pandas as pd
          import numpy as np
          import random
          from tqdm import tqdm

          df_output =

          for i in tqdm(range(1000)):
          sample_matrix = np.random.rand(60,4)
          df = pd.DataFrame(sample_matrix)
          df.columns = ['V_' + str(col) for col in df.columns]
          df_output.append(np.array(df.sum()))

          df_output


          df_output will be a matrix, where the number of rows is 1000 (= the number of simulations)



          enter image description here






          share|improve this answer






























            1














            Without knowing how/why you plan on randomizing each column each iteration, this will work:



            df = pd.DataFrame(np.random.rand(500,4)) # initialize with random data

            iterations = [df.sum()]
            for i in range(999):
            iterations = np.vstack([iterations, df.sum()])

            iterations = pd.DataFrame(iterations)





            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%2f53286656%2fwrite-python-simulation-output-to-a-matrix%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














              Take the sum of four columns in a pandas dataframe (which are determined by a random number) and simulate this process 1000 times. This should give me 1000 rows each with different results, for each column. We can write:



              import os
              import pandas as pd
              import numpy as np
              import random
              from tqdm import tqdm

              df_output =

              for i in tqdm(range(1000)):
              sample_matrix = np.random.rand(60,4)
              df = pd.DataFrame(sample_matrix)
              df.columns = ['V_' + str(col) for col in df.columns]
              df_output.append(np.array(df.sum()))

              df_output


              df_output will be a matrix, where the number of rows is 1000 (= the number of simulations)



              enter image description here






              share|improve this answer



























                1














                Take the sum of four columns in a pandas dataframe (which are determined by a random number) and simulate this process 1000 times. This should give me 1000 rows each with different results, for each column. We can write:



                import os
                import pandas as pd
                import numpy as np
                import random
                from tqdm import tqdm

                df_output =

                for i in tqdm(range(1000)):
                sample_matrix = np.random.rand(60,4)
                df = pd.DataFrame(sample_matrix)
                df.columns = ['V_' + str(col) for col in df.columns]
                df_output.append(np.array(df.sum()))

                df_output


                df_output will be a matrix, where the number of rows is 1000 (= the number of simulations)



                enter image description here






                share|improve this answer

























                  1












                  1








                  1







                  Take the sum of four columns in a pandas dataframe (which are determined by a random number) and simulate this process 1000 times. This should give me 1000 rows each with different results, for each column. We can write:



                  import os
                  import pandas as pd
                  import numpy as np
                  import random
                  from tqdm import tqdm

                  df_output =

                  for i in tqdm(range(1000)):
                  sample_matrix = np.random.rand(60,4)
                  df = pd.DataFrame(sample_matrix)
                  df.columns = ['V_' + str(col) for col in df.columns]
                  df_output.append(np.array(df.sum()))

                  df_output


                  df_output will be a matrix, where the number of rows is 1000 (= the number of simulations)



                  enter image description here






                  share|improve this answer













                  Take the sum of four columns in a pandas dataframe (which are determined by a random number) and simulate this process 1000 times. This should give me 1000 rows each with different results, for each column. We can write:



                  import os
                  import pandas as pd
                  import numpy as np
                  import random
                  from tqdm import tqdm

                  df_output =

                  for i in tqdm(range(1000)):
                  sample_matrix = np.random.rand(60,4)
                  df = pd.DataFrame(sample_matrix)
                  df.columns = ['V_' + str(col) for col in df.columns]
                  df_output.append(np.array(df.sum()))

                  df_output


                  df_output will be a matrix, where the number of rows is 1000 (= the number of simulations)



                  enter image description here







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 13 '18 at 18:15









                  kon_ukon_u

                  1966




                  1966























                      1














                      Without knowing how/why you plan on randomizing each column each iteration, this will work:



                      df = pd.DataFrame(np.random.rand(500,4)) # initialize with random data

                      iterations = [df.sum()]
                      for i in range(999):
                      iterations = np.vstack([iterations, df.sum()])

                      iterations = pd.DataFrame(iterations)





                      share|improve this answer



























                        1














                        Without knowing how/why you plan on randomizing each column each iteration, this will work:



                        df = pd.DataFrame(np.random.rand(500,4)) # initialize with random data

                        iterations = [df.sum()]
                        for i in range(999):
                        iterations = np.vstack([iterations, df.sum()])

                        iterations = pd.DataFrame(iterations)





                        share|improve this answer

























                          1












                          1








                          1







                          Without knowing how/why you plan on randomizing each column each iteration, this will work:



                          df = pd.DataFrame(np.random.rand(500,4)) # initialize with random data

                          iterations = [df.sum()]
                          for i in range(999):
                          iterations = np.vstack([iterations, df.sum()])

                          iterations = pd.DataFrame(iterations)





                          share|improve this answer













                          Without knowing how/why you plan on randomizing each column each iteration, this will work:



                          df = pd.DataFrame(np.random.rand(500,4)) # initialize with random data

                          iterations = [df.sum()]
                          for i in range(999):
                          iterations = np.vstack([iterations, df.sum()])

                          iterations = pd.DataFrame(iterations)






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 13 '18 at 21:51









                          Brian JosephBrian Joseph

                          5109




                          5109



























                              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%2f53286656%2fwrite-python-simulation-output-to-a-matrix%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

                              Top Tejano songwriter Luis Silva dead of heart attack at 64

                              政党

                              天津地下鉄3号線