How to Write Multiple Data Frames in an Excel SheetDo data scientists use Excel?How do I merge two data...

what does しにみえてる mean?

How can I get my players to come to the game session after agreeing to a date?

Who is this Ant Woman character in this image alongside the Wasp?

Is a debit card dangerous in my situation?

Am I a Rude Number?

How can I deliver in-universe written lore to players without it being dry exposition?

How do you funnel food off a cutting board?

How to prevent cleaner from hanging my lock screen in Ubuntu 16.04

Explain the objections to these measures against human trafficking

Eww, those bytes are gross

Is there any differences between "Gucken" and "Schauen"?

What is 6÷2×(1+2) =?

Error in a formula field

Can an insurance company drop you after receiving a bill and refusing to pay?

How to remove blank page before my new report chapter?

How can my powered armor quickly replace its ceramic plates?

How do Chazal know that the descendants of a Mamzer may never marry into the general populace?

How to prevent users from executing commands through browser URL

Why would the Pakistan airspace closure cancel flights not headed to Pakistan itself?

Can we use the stored gravitational potential energy of a building to produce power?

Would a National Army of mercenaries be a feasible idea?

Roman Numerals equation 1

Is it a fallacy if someone claims they need an explanation for every word of your argument to the point where they don't understand common terms?

What kind of hardware implements Fourier transform?



How to Write Multiple Data Frames in an Excel Sheet


Do data scientists use Excel?How do I merge two data frames in Python Pandas?Excel formula questionIs Excel sufficient for data science?Create new data frames from existing data frame based on unique column valuesCombine Pandas DataFrames with year columnsHow do I compare columns in different data frames?Spearmanr on two pandas dataframesExcel All Possible Combinations for MacOSCategorical data into numeric in excel













1












$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$

















    1












    $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$















      1












      1








      1





      $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 5 hours ago







      Della

















      asked 5 hours ago









      DellaDella

      1104




      1104






















          3 Answers
          3






          active

          oldest

          votes


















          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








          New contributor




          chacid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          $endgroup$





















            2












            $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








            New contributor




            Frenchy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            $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









                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








                New contributor




                chacid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






                $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








                  New contributor




                  chacid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                  Check out our Code of Conduct.






                  $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








                    New contributor




                    chacid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.






                    $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








                    New contributor




                    chacid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.









                    share|improve this answer



                    share|improve this answer






                    New contributor




                    chacid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.









                    answered 2 hours ago









                    chacidchacid

                    616




                    616




                    New contributor




                    chacid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.





                    New contributor





                    chacid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.






                    chacid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                    Check out our Code of Conduct.























                        2












                        $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








                        New contributor




                        Frenchy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.






                        $endgroup$


















                          2












                          $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








                          New contributor




                          Frenchy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.






                          $endgroup$
















                            2












                            2








                            2





                            $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








                            New contributor




                            Frenchy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.






                            $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








                            New contributor




                            Frenchy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.









                            share|improve this answer



                            share|improve this answer






                            New contributor




                            Frenchy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.









                            answered 1 hour ago









                            FrenchyFrenchy

                            1213




                            1213




                            New contributor




                            Frenchy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.





                            New contributor





                            Frenchy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.






                            Frenchy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.























                                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 1 hour ago









                                    ItachiItachi

                                    1613




                                    1613






























                                        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

                                        “%fieldName is a required field.”, in Magento2 REST API Call for GET Method Type The Next...

                                        How to change City field to a dropdown in Checkout step Magento 2Magento 2 : How to change UI field(s)...

                                        變成蝙蝠會怎樣? 參考資料 外部連結 导航菜单Thomas Nagel, "What is it like to be a...