How to Write Multiple Data Frames in an Excel Sheet












3












$begingroup$


I have multiple data frames with same column names. I want to write them together to an excel sheet stacked vertically on top of each other. And between each, there will be a text occupying a row. This is what I have in mind.



enter image description here



I tried the pandas.ExcelWriter() method, but each dataframe overwrites the previous frame in the sheet, instead of appending.



Note that, I still need multiple sheets for different dataframe, but also multiple dataframes on each sheet. Is it possible? Or any other python library which can dynamically generate the excel sheet from pandas dataframes?










share|improve this question











$endgroup$

















    3












    $begingroup$


    I have multiple data frames with same column names. I want to write them together to an excel sheet stacked vertically on top of each other. And between each, there will be a text occupying a row. This is what I have in mind.



    enter image description here



    I tried the pandas.ExcelWriter() method, but each dataframe overwrites the previous frame in the sheet, instead of appending.



    Note that, I still need multiple sheets for different dataframe, but also multiple dataframes on each sheet. Is it possible? Or any other python library which can dynamically generate the excel sheet from pandas dataframes?










    share|improve this question











    $endgroup$















      3












      3








      3





      $begingroup$


      I have multiple data frames with same column names. I want to write them together to an excel sheet stacked vertically on top of each other. And between each, there will be a text occupying a row. This is what I have in mind.



      enter image description here



      I tried the pandas.ExcelWriter() method, but each dataframe overwrites the previous frame in the sheet, instead of appending.



      Note that, I still need multiple sheets for different dataframe, but also multiple dataframes on each sheet. Is it possible? Or any other python library which can dynamically generate the excel sheet from pandas dataframes?










      share|improve this question











      $endgroup$




      I have multiple data frames with same column names. I want to write them together to an excel sheet stacked vertically on top of each other. And between each, there will be a text occupying a row. This is what I have in mind.



      enter image description here



      I tried the pandas.ExcelWriter() method, but each dataframe overwrites the previous frame in the sheet, instead of appending.



      Note that, I still need multiple sheets for different dataframe, but also multiple dataframes on each sheet. Is it possible? Or any other python library which can dynamically generate the excel sheet from pandas dataframes?







      pandas dataframe excel data.table






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 1 at 5:41







      Della

















      asked Mar 1 at 5:23









      DellaDella

      1245




      1245






















          3 Answers
          3






          active

          oldest

          votes


















          4












          $begingroup$

          an example to write in same sheet:



          import pandas as pd

          data1 = """
          class precision recall
          <18 0.0125 12
          18-24 0.0250 16
          25-34 0.00350 4
          """
          data2 = """
          class precision recall
          <18 0 0
          18-24 0.25 6
          25-34 0.35 5
          """

          #create 2 df for sample
          df1 = pd.read_csv(pd.compat.StringIO(data1), sep='s+')
          df1.name = "Dataframe1"
          df2 = pd.read_csv(pd.compat.StringIO(data2), sep='s+')
          df2.name = "Dataframe2"
          print(df1);print(df2)

          writer = pd.ExcelWriter('e:\test.xlsx',engine='xlsxwriter')
          workbook=writer.book
          worksheet=workbook.add_worksheet('Result')
          writer.sheets['Result'] = worksheet
          worksheet.write_string(0, 0, df1.name)

          df1.to_excel(writer,sheet_name='Result',startrow=1 , startcol=0)
          worksheet.write_string(df1.shape[0] + 4, 0, df2.name)
          df2.to_excel(writer,sheet_name='Result',startrow=df1.shape[0] + 5, startcol=0)


          output:



          enter image description here



          if you want to write in different sheets:



          import pandas as pd

          # Create a Pandas Excel writer using XlsxWriter as the engine.
          writer = pd.ExcelWriter('e:\test.xlsx', engine='xlsxwriter')

          # Write each dataframe to a different worksheet. you could write different string like above if you want
          df1.to_excel(writer, sheet_name='Sheet1')
          df2.to_excel(writer, sheet_name='Sheet2')

          # Close the Pandas Excel writer and output the Excel file.
          writer.save()





          share|improve this answer









          $endgroup$





















            2












            $begingroup$

            Several dataframes to same sheet from here and here with selected sheet:



            # Position the dataframes in the worksheet.
            df1.to_excel(writer, sheet_name='Sheet1') # Default position, cell A1.
            df2.to_excel(writer, sheet_name='Sheet1', startcol=3)
            df3.to_excel(writer, sheet_name='Sheet1', startrow=6)





            share|improve this answer









            $endgroup$





















              0












              $begingroup$

              You can open the excel editor and write to it and then save



              writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
              data.to_excel(writer, sheet_name='Sheet1',
              encoding='utf-8', index=False)
              writer.save()


              Please refer this answer https://stackoverflow.com/questions/34744863/python-how-to-use-excelwriter-to-write-into-an-existing-worksheet






              share|improve this answer









              $endgroup$














                Your Answer





                StackExchange.ifUsing("editor", function () {
                return StackExchange.using("mathjaxEditing", function () {
                StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
                StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
                });
                });
                }, "mathjax-editing");

                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "557"
                };
                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: false,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                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%2fdatascience.stackexchange.com%2fquestions%2f46437%2fhow-to-write-multiple-data-frames-in-an-excel-sheet%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                4












                $begingroup$

                an example to write in same sheet:



                import pandas as pd

                data1 = """
                class precision recall
                <18 0.0125 12
                18-24 0.0250 16
                25-34 0.00350 4
                """
                data2 = """
                class precision recall
                <18 0 0
                18-24 0.25 6
                25-34 0.35 5
                """

                #create 2 df for sample
                df1 = pd.read_csv(pd.compat.StringIO(data1), sep='s+')
                df1.name = "Dataframe1"
                df2 = pd.read_csv(pd.compat.StringIO(data2), sep='s+')
                df2.name = "Dataframe2"
                print(df1);print(df2)

                writer = pd.ExcelWriter('e:\test.xlsx',engine='xlsxwriter')
                workbook=writer.book
                worksheet=workbook.add_worksheet('Result')
                writer.sheets['Result'] = worksheet
                worksheet.write_string(0, 0, df1.name)

                df1.to_excel(writer,sheet_name='Result',startrow=1 , startcol=0)
                worksheet.write_string(df1.shape[0] + 4, 0, df2.name)
                df2.to_excel(writer,sheet_name='Result',startrow=df1.shape[0] + 5, startcol=0)


                output:



                enter image description here



                if you want to write in different sheets:



                import pandas as pd

                # Create a Pandas Excel writer using XlsxWriter as the engine.
                writer = pd.ExcelWriter('e:\test.xlsx', engine='xlsxwriter')

                # Write each dataframe to a different worksheet. you could write different string like above if you want
                df1.to_excel(writer, sheet_name='Sheet1')
                df2.to_excel(writer, sheet_name='Sheet2')

                # Close the Pandas Excel writer and output the Excel file.
                writer.save()





                share|improve this answer









                $endgroup$


















                  4












                  $begingroup$

                  an example to write in same sheet:



                  import pandas as pd

                  data1 = """
                  class precision recall
                  <18 0.0125 12
                  18-24 0.0250 16
                  25-34 0.00350 4
                  """
                  data2 = """
                  class precision recall
                  <18 0 0
                  18-24 0.25 6
                  25-34 0.35 5
                  """

                  #create 2 df for sample
                  df1 = pd.read_csv(pd.compat.StringIO(data1), sep='s+')
                  df1.name = "Dataframe1"
                  df2 = pd.read_csv(pd.compat.StringIO(data2), sep='s+')
                  df2.name = "Dataframe2"
                  print(df1);print(df2)

                  writer = pd.ExcelWriter('e:\test.xlsx',engine='xlsxwriter')
                  workbook=writer.book
                  worksheet=workbook.add_worksheet('Result')
                  writer.sheets['Result'] = worksheet
                  worksheet.write_string(0, 0, df1.name)

                  df1.to_excel(writer,sheet_name='Result',startrow=1 , startcol=0)
                  worksheet.write_string(df1.shape[0] + 4, 0, df2.name)
                  df2.to_excel(writer,sheet_name='Result',startrow=df1.shape[0] + 5, startcol=0)


                  output:



                  enter image description here



                  if you want to write in different sheets:



                  import pandas as pd

                  # Create a Pandas Excel writer using XlsxWriter as the engine.
                  writer = pd.ExcelWriter('e:\test.xlsx', engine='xlsxwriter')

                  # Write each dataframe to a different worksheet. you could write different string like above if you want
                  df1.to_excel(writer, sheet_name='Sheet1')
                  df2.to_excel(writer, sheet_name='Sheet2')

                  # Close the Pandas Excel writer and output the Excel file.
                  writer.save()





                  share|improve this answer









                  $endgroup$
















                    4












                    4








                    4





                    $begingroup$

                    an example to write in same sheet:



                    import pandas as pd

                    data1 = """
                    class precision recall
                    <18 0.0125 12
                    18-24 0.0250 16
                    25-34 0.00350 4
                    """
                    data2 = """
                    class precision recall
                    <18 0 0
                    18-24 0.25 6
                    25-34 0.35 5
                    """

                    #create 2 df for sample
                    df1 = pd.read_csv(pd.compat.StringIO(data1), sep='s+')
                    df1.name = "Dataframe1"
                    df2 = pd.read_csv(pd.compat.StringIO(data2), sep='s+')
                    df2.name = "Dataframe2"
                    print(df1);print(df2)

                    writer = pd.ExcelWriter('e:\test.xlsx',engine='xlsxwriter')
                    workbook=writer.book
                    worksheet=workbook.add_worksheet('Result')
                    writer.sheets['Result'] = worksheet
                    worksheet.write_string(0, 0, df1.name)

                    df1.to_excel(writer,sheet_name='Result',startrow=1 , startcol=0)
                    worksheet.write_string(df1.shape[0] + 4, 0, df2.name)
                    df2.to_excel(writer,sheet_name='Result',startrow=df1.shape[0] + 5, startcol=0)


                    output:



                    enter image description here



                    if you want to write in different sheets:



                    import pandas as pd

                    # Create a Pandas Excel writer using XlsxWriter as the engine.
                    writer = pd.ExcelWriter('e:\test.xlsx', engine='xlsxwriter')

                    # Write each dataframe to a different worksheet. you could write different string like above if you want
                    df1.to_excel(writer, sheet_name='Sheet1')
                    df2.to_excel(writer, sheet_name='Sheet2')

                    # Close the Pandas Excel writer and output the Excel file.
                    writer.save()





                    share|improve this answer









                    $endgroup$



                    an example to write in same sheet:



                    import pandas as pd

                    data1 = """
                    class precision recall
                    <18 0.0125 12
                    18-24 0.0250 16
                    25-34 0.00350 4
                    """
                    data2 = """
                    class precision recall
                    <18 0 0
                    18-24 0.25 6
                    25-34 0.35 5
                    """

                    #create 2 df for sample
                    df1 = pd.read_csv(pd.compat.StringIO(data1), sep='s+')
                    df1.name = "Dataframe1"
                    df2 = pd.read_csv(pd.compat.StringIO(data2), sep='s+')
                    df2.name = "Dataframe2"
                    print(df1);print(df2)

                    writer = pd.ExcelWriter('e:\test.xlsx',engine='xlsxwriter')
                    workbook=writer.book
                    worksheet=workbook.add_worksheet('Result')
                    writer.sheets['Result'] = worksheet
                    worksheet.write_string(0, 0, df1.name)

                    df1.to_excel(writer,sheet_name='Result',startrow=1 , startcol=0)
                    worksheet.write_string(df1.shape[0] + 4, 0, df2.name)
                    df2.to_excel(writer,sheet_name='Result',startrow=df1.shape[0] + 5, startcol=0)


                    output:



                    enter image description here



                    if you want to write in different sheets:



                    import pandas as pd

                    # Create a Pandas Excel writer using XlsxWriter as the engine.
                    writer = pd.ExcelWriter('e:\test.xlsx', engine='xlsxwriter')

                    # Write each dataframe to a different worksheet. you could write different string like above if you want
                    df1.to_excel(writer, sheet_name='Sheet1')
                    df2.to_excel(writer, sheet_name='Sheet2')

                    # Close the Pandas Excel writer and output the Excel file.
                    writer.save()






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 1 at 8:56









                    FrenchyFrenchy

                    1715




                    1715























                        2












                        $begingroup$

                        Several dataframes to same sheet from here and here with selected sheet:



                        # Position the dataframes in the worksheet.
                        df1.to_excel(writer, sheet_name='Sheet1') # Default position, cell A1.
                        df2.to_excel(writer, sheet_name='Sheet1', startcol=3)
                        df3.to_excel(writer, sheet_name='Sheet1', startrow=6)





                        share|improve this answer









                        $endgroup$


















                          2












                          $begingroup$

                          Several dataframes to same sheet from here and here with selected sheet:



                          # Position the dataframes in the worksheet.
                          df1.to_excel(writer, sheet_name='Sheet1') # Default position, cell A1.
                          df2.to_excel(writer, sheet_name='Sheet1', startcol=3)
                          df3.to_excel(writer, sheet_name='Sheet1', startrow=6)





                          share|improve this answer









                          $endgroup$
















                            2












                            2








                            2





                            $begingroup$

                            Several dataframes to same sheet from here and here with selected sheet:



                            # Position the dataframes in the worksheet.
                            df1.to_excel(writer, sheet_name='Sheet1') # Default position, cell A1.
                            df2.to_excel(writer, sheet_name='Sheet1', startcol=3)
                            df3.to_excel(writer, sheet_name='Sheet1', startrow=6)





                            share|improve this answer









                            $endgroup$



                            Several dataframes to same sheet from here and here with selected sheet:



                            # Position the dataframes in the worksheet.
                            df1.to_excel(writer, sheet_name='Sheet1') # Default position, cell A1.
                            df2.to_excel(writer, sheet_name='Sheet1', startcol=3)
                            df3.to_excel(writer, sheet_name='Sheet1', startrow=6)






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 1 at 8:44









                            chacidchacid

                            616




                            616























                                0












                                $begingroup$

                                You can open the excel editor and write to it and then save



                                writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
                                data.to_excel(writer, sheet_name='Sheet1',
                                encoding='utf-8', index=False)
                                writer.save()


                                Please refer this answer https://stackoverflow.com/questions/34744863/python-how-to-use-excelwriter-to-write-into-an-existing-worksheet






                                share|improve this answer









                                $endgroup$


















                                  0












                                  $begingroup$

                                  You can open the excel editor and write to it and then save



                                  writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
                                  data.to_excel(writer, sheet_name='Sheet1',
                                  encoding='utf-8', index=False)
                                  writer.save()


                                  Please refer this answer https://stackoverflow.com/questions/34744863/python-how-to-use-excelwriter-to-write-into-an-existing-worksheet






                                  share|improve this answer









                                  $endgroup$
















                                    0












                                    0








                                    0





                                    $begingroup$

                                    You can open the excel editor and write to it and then save



                                    writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
                                    data.to_excel(writer, sheet_name='Sheet1',
                                    encoding='utf-8', index=False)
                                    writer.save()


                                    Please refer this answer https://stackoverflow.com/questions/34744863/python-how-to-use-excelwriter-to-write-into-an-existing-worksheet






                                    share|improve this answer









                                    $endgroup$



                                    You can open the excel editor and write to it and then save



                                    writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
                                    data.to_excel(writer, sheet_name='Sheet1',
                                    encoding='utf-8', index=False)
                                    writer.save()


                                    Please refer this answer https://stackoverflow.com/questions/34744863/python-how-to-use-excelwriter-to-write-into-an-existing-worksheet







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Mar 1 at 9:21









                                    ItachiItachi

                                    1713




                                    1713






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Data Science Stack Exchange!


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


                                        Use MathJax to format equations. MathJax reference.


                                        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%2fdatascience.stackexchange.com%2fquestions%2f46437%2fhow-to-write-multiple-data-frames-in-an-excel-sheet%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

                                        How do I know what Microsoft account the skydrive app is syncing to?

                                        When does type information flow backwards in C++?

                                        Grease: Live!