Exporting a list as a new column in a pandas dataframe as part of a nested for loopAdd one row to pandas DataFrameSelecting multiple columns in a pandas dataframeRenaming columns in pandasAdding new column to existing DataFrame in Python pandasDelete column from pandas DataFrame“Large data” work flows using pandasHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headerscombining 2 pandas dataframes

Output Distinct Factor Cuboids

How to give my students a straightedge instead of a ruler

What to do as a player when ranger animal companion dies

Applications of mathematics in clinical setting

Other than good shoes and a stick, what are some ways to preserve your knees on long hikes?

What is the maximum viable speed for a projectile within earth's atmosphere?

Are there any instances in Tanach of Lashon Hara said purely for non-constructive purposes?

Unpredictability of Stock Market

Whence comes increasing usage of "do" for "have" in ordering food?

Can a business put whatever they want into a contract?

Should the pagination be reset when changing the order?

Did slaves have slaves?

How to convey to the people around me that I want to disengage myself from constant giving?

Social Encounters in a West Marches Campaign

How can I check that parent has more than 1 child?

Delete empty subfolders, keep parent folder

What was the deeper meaning of Hermione wanting the cloak?

Minimum number of lines to draw 111 squares

What exactly is a web font, and what does converting to one involve?

Can one guy with a duplicator trigger a nuclear apocalypse?

What's the benefit of prohibiting the use of techniques/language constructs that have not been taught?

Why are there no programmes / playbills for movies?

Could the Orion project pusher plate model be used for asteroid deflection?

Are lay articles good enough to be the main source of information for PhD research?



Exporting a list as a new column in a pandas dataframe as part of a nested for loop


Add one row to pandas DataFrameSelecting multiple columns in a pandas dataframeRenaming columns in pandasAdding new column to existing DataFrame in Python pandasDelete column from pandas DataFrame“Large data” work flows using pandasHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headerscombining 2 pandas dataframes






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I am inputting multiple spreadsheets with multiple columns of data. For each spreadsheet, the maximum value of each column is found. Then, for each element in the column, the element is divided by the maximum value of that column. The output should be a value (between 0 and 1) for each element in the column in ascending order. This is appended to a list which should be added to the source spreadsheet as a column.



Currently, the nested loops are performing correctly apart from the final step, as far as I understand. Each column is added to the spreadsheet EXCEPT the values are for the final column of the source spreadsheet rather than values related to each individual column.



I have tried changing the indents to associate levels of the code with different parts (as I think this is the problem) and tried moving the appended column along in the dataframe, to no avail.



for i in distlist:
#listname = i[4:] + '_norm'
df2 = pd.read_excel(i,header=0,index_col=None, skip_blank_lines=True)
df3 = df2.dropna(axis=0, how='any')



cols = []
for column in df3:
cols.append(column)

for x in cols:
listname = x + ' norm'
maxval = df3[x].max()
print(maxval)
mylist = []

for j in df3[x]:
findNL = (j/maxval)
mylist.append(findNL)
df3[listname] = mylist


saveloc = 'E:/test/'
filename = i[:-18] + '_Normalised.xlsx'
df3.to_excel(saveloc+filename, index=False)


New columns are added to the output dataframe with bespoke headings relating to the field headers in the source spreadsheet and renamed according to (listname). The data in each one of these new columns is identical and relates to the final column in the spreadsheet. To me, it seems to be overwriting the values each time (as if looping through the entire spreadsheet, not outputting for each column), and adding it to the spreadsheet.



Any help would be much appreciated. I think it's something simple, but I haven't managed to work out what...










share|improve this question






























    0















    I am inputting multiple spreadsheets with multiple columns of data. For each spreadsheet, the maximum value of each column is found. Then, for each element in the column, the element is divided by the maximum value of that column. The output should be a value (between 0 and 1) for each element in the column in ascending order. This is appended to a list which should be added to the source spreadsheet as a column.



    Currently, the nested loops are performing correctly apart from the final step, as far as I understand. Each column is added to the spreadsheet EXCEPT the values are for the final column of the source spreadsheet rather than values related to each individual column.



    I have tried changing the indents to associate levels of the code with different parts (as I think this is the problem) and tried moving the appended column along in the dataframe, to no avail.



    for i in distlist:
    #listname = i[4:] + '_norm'
    df2 = pd.read_excel(i,header=0,index_col=None, skip_blank_lines=True)
    df3 = df2.dropna(axis=0, how='any')



    cols = []
    for column in df3:
    cols.append(column)

    for x in cols:
    listname = x + ' norm'
    maxval = df3[x].max()
    print(maxval)
    mylist = []

    for j in df3[x]:
    findNL = (j/maxval)
    mylist.append(findNL)
    df3[listname] = mylist


    saveloc = 'E:/test/'
    filename = i[:-18] + '_Normalised.xlsx'
    df3.to_excel(saveloc+filename, index=False)


    New columns are added to the output dataframe with bespoke headings relating to the field headers in the source spreadsheet and renamed according to (listname). The data in each one of these new columns is identical and relates to the final column in the spreadsheet. To me, it seems to be overwriting the values each time (as if looping through the entire spreadsheet, not outputting for each column), and adding it to the spreadsheet.



    Any help would be much appreciated. I think it's something simple, but I haven't managed to work out what...










    share|improve this question


























      0












      0








      0








      I am inputting multiple spreadsheets with multiple columns of data. For each spreadsheet, the maximum value of each column is found. Then, for each element in the column, the element is divided by the maximum value of that column. The output should be a value (between 0 and 1) for each element in the column in ascending order. This is appended to a list which should be added to the source spreadsheet as a column.



      Currently, the nested loops are performing correctly apart from the final step, as far as I understand. Each column is added to the spreadsheet EXCEPT the values are for the final column of the source spreadsheet rather than values related to each individual column.



      I have tried changing the indents to associate levels of the code with different parts (as I think this is the problem) and tried moving the appended column along in the dataframe, to no avail.



      for i in distlist:
      #listname = i[4:] + '_norm'
      df2 = pd.read_excel(i,header=0,index_col=None, skip_blank_lines=True)
      df3 = df2.dropna(axis=0, how='any')



      cols = []
      for column in df3:
      cols.append(column)

      for x in cols:
      listname = x + ' norm'
      maxval = df3[x].max()
      print(maxval)
      mylist = []

      for j in df3[x]:
      findNL = (j/maxval)
      mylist.append(findNL)
      df3[listname] = mylist


      saveloc = 'E:/test/'
      filename = i[:-18] + '_Normalised.xlsx'
      df3.to_excel(saveloc+filename, index=False)


      New columns are added to the output dataframe with bespoke headings relating to the field headers in the source spreadsheet and renamed according to (listname). The data in each one of these new columns is identical and relates to the final column in the spreadsheet. To me, it seems to be overwriting the values each time (as if looping through the entire spreadsheet, not outputting for each column), and adding it to the spreadsheet.



      Any help would be much appreciated. I think it's something simple, but I haven't managed to work out what...










      share|improve this question














      I am inputting multiple spreadsheets with multiple columns of data. For each spreadsheet, the maximum value of each column is found. Then, for each element in the column, the element is divided by the maximum value of that column. The output should be a value (between 0 and 1) for each element in the column in ascending order. This is appended to a list which should be added to the source spreadsheet as a column.



      Currently, the nested loops are performing correctly apart from the final step, as far as I understand. Each column is added to the spreadsheet EXCEPT the values are for the final column of the source spreadsheet rather than values related to each individual column.



      I have tried changing the indents to associate levels of the code with different parts (as I think this is the problem) and tried moving the appended column along in the dataframe, to no avail.



      for i in distlist:
      #listname = i[4:] + '_norm'
      df2 = pd.read_excel(i,header=0,index_col=None, skip_blank_lines=True)
      df3 = df2.dropna(axis=0, how='any')



      cols = []
      for column in df3:
      cols.append(column)

      for x in cols:
      listname = x + ' norm'
      maxval = df3[x].max()
      print(maxval)
      mylist = []

      for j in df3[x]:
      findNL = (j/maxval)
      mylist.append(findNL)
      df3[listname] = mylist


      saveloc = 'E:/test/'
      filename = i[:-18] + '_Normalised.xlsx'
      df3.to_excel(saveloc+filename, index=False)


      New columns are added to the output dataframe with bespoke headings relating to the field headers in the source spreadsheet and renamed according to (listname). The data in each one of these new columns is identical and relates to the final column in the spreadsheet. To me, it seems to be overwriting the values each time (as if looping through the entire spreadsheet, not outputting for each column), and adding it to the spreadsheet.



      Any help would be much appreciated. I think it's something simple, but I haven't managed to work out what...







      python-3.x pandas for-loop nested-loops






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 28 at 13:36









      GeomorphicJoshGeomorphicJosh

      83 bronze badges




      83 bronze badges

























          2 Answers
          2






          active

          oldest

          votes


















          0
















          If I understand you correctly, you are overcomplicating things. You dont need a for loop for this. You can simplify your code:



          # Make example dataframe, this is not provided
          df = pd.DataFrame('col1':[1, 2, 3, 4],
          'col2':[5, 6, 7, 8])

          print(df)
          col1 col2
          0 1 5
          1 2 6
          2 3 7
          3 4 8


          Now we can use DataFrame.apply and use add_suffix to give the new columns _norm suffix and after that concat the columns to one final dataframe



          df_conc = pd.concat([df, df.apply(lambda x: x/x.max()).add_suffix('_norm')],axis=1)

          print(df_conc)
          col1 col2 col1_norm col2_norm
          0 1 5 0.25 0.625
          1 2 6 0.50 0.750
          2 3 7 0.75 0.875
          3 4 8 1.00 1.000





          share|improve this answer
































            0
















            Many thanks. I think I was just overcomplicating it. Incidentally, I think my code may do the same job, but because there is so little difference in the values, it wasn't notable.



            Thanks for your help @Erfan






            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/4.0/"u003ecc by-sa 4.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%2f55398997%2fexporting-a-list-as-a-new-column-in-a-pandas-dataframe-as-part-of-a-nested-for-l%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









              0
















              If I understand you correctly, you are overcomplicating things. You dont need a for loop for this. You can simplify your code:



              # Make example dataframe, this is not provided
              df = pd.DataFrame('col1':[1, 2, 3, 4],
              'col2':[5, 6, 7, 8])

              print(df)
              col1 col2
              0 1 5
              1 2 6
              2 3 7
              3 4 8


              Now we can use DataFrame.apply and use add_suffix to give the new columns _norm suffix and after that concat the columns to one final dataframe



              df_conc = pd.concat([df, df.apply(lambda x: x/x.max()).add_suffix('_norm')],axis=1)

              print(df_conc)
              col1 col2 col1_norm col2_norm
              0 1 5 0.25 0.625
              1 2 6 0.50 0.750
              2 3 7 0.75 0.875
              3 4 8 1.00 1.000





              share|improve this answer





























                0
















                If I understand you correctly, you are overcomplicating things. You dont need a for loop for this. You can simplify your code:



                # Make example dataframe, this is not provided
                df = pd.DataFrame('col1':[1, 2, 3, 4],
                'col2':[5, 6, 7, 8])

                print(df)
                col1 col2
                0 1 5
                1 2 6
                2 3 7
                3 4 8


                Now we can use DataFrame.apply and use add_suffix to give the new columns _norm suffix and after that concat the columns to one final dataframe



                df_conc = pd.concat([df, df.apply(lambda x: x/x.max()).add_suffix('_norm')],axis=1)

                print(df_conc)
                col1 col2 col1_norm col2_norm
                0 1 5 0.25 0.625
                1 2 6 0.50 0.750
                2 3 7 0.75 0.875
                3 4 8 1.00 1.000





                share|improve this answer



























                  0














                  0










                  0









                  If I understand you correctly, you are overcomplicating things. You dont need a for loop for this. You can simplify your code:



                  # Make example dataframe, this is not provided
                  df = pd.DataFrame('col1':[1, 2, 3, 4],
                  'col2':[5, 6, 7, 8])

                  print(df)
                  col1 col2
                  0 1 5
                  1 2 6
                  2 3 7
                  3 4 8


                  Now we can use DataFrame.apply and use add_suffix to give the new columns _norm suffix and after that concat the columns to one final dataframe



                  df_conc = pd.concat([df, df.apply(lambda x: x/x.max()).add_suffix('_norm')],axis=1)

                  print(df_conc)
                  col1 col2 col1_norm col2_norm
                  0 1 5 0.25 0.625
                  1 2 6 0.50 0.750
                  2 3 7 0.75 0.875
                  3 4 8 1.00 1.000





                  share|improve this answer













                  If I understand you correctly, you are overcomplicating things. You dont need a for loop for this. You can simplify your code:



                  # Make example dataframe, this is not provided
                  df = pd.DataFrame('col1':[1, 2, 3, 4],
                  'col2':[5, 6, 7, 8])

                  print(df)
                  col1 col2
                  0 1 5
                  1 2 6
                  2 3 7
                  3 4 8


                  Now we can use DataFrame.apply and use add_suffix to give the new columns _norm suffix and after that concat the columns to one final dataframe



                  df_conc = pd.concat([df, df.apply(lambda x: x/x.max()).add_suffix('_norm')],axis=1)

                  print(df_conc)
                  col1 col2 col1_norm col2_norm
                  0 1 5 0.25 0.625
                  1 2 6 0.50 0.750
                  2 3 7 0.75 0.875
                  3 4 8 1.00 1.000






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 28 at 13:46









                  ErfanErfan

                  12k2 gold badges8 silver badges28 bronze badges




                  12k2 gold badges8 silver badges28 bronze badges


























                      0
















                      Many thanks. I think I was just overcomplicating it. Incidentally, I think my code may do the same job, but because there is so little difference in the values, it wasn't notable.



                      Thanks for your help @Erfan






                      share|improve this answer





























                        0
















                        Many thanks. I think I was just overcomplicating it. Incidentally, I think my code may do the same job, but because there is so little difference in the values, it wasn't notable.



                        Thanks for your help @Erfan






                        share|improve this answer



























                          0














                          0










                          0









                          Many thanks. I think I was just overcomplicating it. Incidentally, I think my code may do the same job, but because there is so little difference in the values, it wasn't notable.



                          Thanks for your help @Erfan






                          share|improve this answer













                          Many thanks. I think I was just overcomplicating it. Incidentally, I think my code may do the same job, but because there is so little difference in the values, it wasn't notable.



                          Thanks for your help @Erfan







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Mar 28 at 15:57









                          GeomorphicJoshGeomorphicJosh

                          83 bronze badges




                          83 bronze badges































                              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%2f55398997%2fexporting-a-list-as-a-new-column-in-a-pandas-dataframe-as-part-of-a-nested-for-l%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

                              Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                              SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                              은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현