python copy dictionary into excel












0















I have this dictionary:



dict1 = {"Name": 'Bob' , "Surname": 'red', "Age": 17 }



how can I copy the dictionary into an excel file?
I would like this output:



           Name       Surname          Age
Bob Red 17


thank you!










share|improve this question

























  • You can import a dictionary to a pandas DataFrame and then write the output to an Excel file, as long as you have OpenPyxl installed. You will need to adjust the structure of your input dict though -- I don't know if this is something that is possible for your use case. Side notes: you wouldn't be able to preserve the order of this dictionary structure you have specified without specifically storing the order separately as a dict is similar to a set (unordered). I would avoid using the Python keyword dict to name a variable.

    – Mark
    Nov 21 '18 at 18:45


















0















I have this dictionary:



dict1 = {"Name": 'Bob' , "Surname": 'red', "Age": 17 }



how can I copy the dictionary into an excel file?
I would like this output:



           Name       Surname          Age
Bob Red 17


thank you!










share|improve this question

























  • You can import a dictionary to a pandas DataFrame and then write the output to an Excel file, as long as you have OpenPyxl installed. You will need to adjust the structure of your input dict though -- I don't know if this is something that is possible for your use case. Side notes: you wouldn't be able to preserve the order of this dictionary structure you have specified without specifically storing the order separately as a dict is similar to a set (unordered). I would avoid using the Python keyword dict to name a variable.

    – Mark
    Nov 21 '18 at 18:45
















0












0








0








I have this dictionary:



dict1 = {"Name": 'Bob' , "Surname": 'red', "Age": 17 }



how can I copy the dictionary into an excel file?
I would like this output:



           Name       Surname          Age
Bob Red 17


thank you!










share|improve this question
















I have this dictionary:



dict1 = {"Name": 'Bob' , "Surname": 'red', "Age": 17 }



how can I copy the dictionary into an excel file?
I would like this output:



           Name       Surname          Age
Bob Red 17


thank you!







python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 18:48







luana nastasi

















asked Nov 21 '18 at 18:41









luana nastasiluana nastasi

113




113













  • You can import a dictionary to a pandas DataFrame and then write the output to an Excel file, as long as you have OpenPyxl installed. You will need to adjust the structure of your input dict though -- I don't know if this is something that is possible for your use case. Side notes: you wouldn't be able to preserve the order of this dictionary structure you have specified without specifically storing the order separately as a dict is similar to a set (unordered). I would avoid using the Python keyword dict to name a variable.

    – Mark
    Nov 21 '18 at 18:45





















  • You can import a dictionary to a pandas DataFrame and then write the output to an Excel file, as long as you have OpenPyxl installed. You will need to adjust the structure of your input dict though -- I don't know if this is something that is possible for your use case. Side notes: you wouldn't be able to preserve the order of this dictionary structure you have specified without specifically storing the order separately as a dict is similar to a set (unordered). I would avoid using the Python keyword dict to name a variable.

    – Mark
    Nov 21 '18 at 18:45



















You can import a dictionary to a pandas DataFrame and then write the output to an Excel file, as long as you have OpenPyxl installed. You will need to adjust the structure of your input dict though -- I don't know if this is something that is possible for your use case. Side notes: you wouldn't be able to preserve the order of this dictionary structure you have specified without specifically storing the order separately as a dict is similar to a set (unordered). I would avoid using the Python keyword dict to name a variable.

– Mark
Nov 21 '18 at 18:45







You can import a dictionary to a pandas DataFrame and then write the output to an Excel file, as long as you have OpenPyxl installed. You will need to adjust the structure of your input dict though -- I don't know if this is something that is possible for your use case. Side notes: you wouldn't be able to preserve the order of this dictionary structure you have specified without specifically storing the order separately as a dict is similar to a set (unordered). I would avoid using the Python keyword dict to name a variable.

– Mark
Nov 21 '18 at 18:45














3 Answers
3






active

oldest

votes


















1














import pandas as pd
pd.DataFrame(dict1).to_excel('myfile.xslx')





share|improve this answer

































    0














    If you want to guarantee the heading order (for versions of Python before 3.7), you'll need to specify it in a list. You can use the xlwt library:



    import xlwt
    from datetime import datetime

    wb = xlwt.Workbook()
    ws = wb.add_sheet('My Sheet')

    dict1 = {"Name": 'Bob' , "Surname": 'red', "Age": 17 }
    headings = ['Name', 'Surname', 'Age']
    for i,heading in enumerate(headings):
    ws.write(0, i, heading)
    ws.write(1, i, str(dict1[heading]))

    wb.save('example.xls')





    share|improve this answer































      0














      dict1 = {"Name": 'Bob' , "Surname": 'red', "Age": 17 }


      You can save it in excel by saving it as pandas data frame in 2 different ways



      Long Format



      Converting dictionary to pandas dataframe



      df=pd.DataFrame(list(dict1.items())) #Python 3
      df=pd.DataFrame(dict1.items()) #Python 2


      this will store your data as :



               0    1
      0 Name Bob
      1 Surname red
      2 Age 17


      Wide Format



      Converting dictionary key as columns in pandas



      df=pd.DataFrame(dict1, index=[0])


      This will convert your data in the desired format



         Age Name Surname
      0 17 Bob red


      And finally, you can save your data in excel



      df.to_csv('myfile.csv', index =False)





      share|improve this answer
























      • If it was helpful, then please accept and upvote the answer. Thanks

        – Ankur Gulati
        Nov 21 '18 at 19:15











      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%2f53418622%2fpython-copy-dictionary-into-excel%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









      1














      import pandas as pd
      pd.DataFrame(dict1).to_excel('myfile.xslx')





      share|improve this answer






























        1














        import pandas as pd
        pd.DataFrame(dict1).to_excel('myfile.xslx')





        share|improve this answer




























          1












          1








          1







          import pandas as pd
          pd.DataFrame(dict1).to_excel('myfile.xslx')





          share|improve this answer















          import pandas as pd
          pd.DataFrame(dict1).to_excel('myfile.xslx')






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 '18 at 2:03

























          answered Nov 21 '18 at 18:43









          ConnerConner

          23.4k84568




          23.4k84568

























              0














              If you want to guarantee the heading order (for versions of Python before 3.7), you'll need to specify it in a list. You can use the xlwt library:



              import xlwt
              from datetime import datetime

              wb = xlwt.Workbook()
              ws = wb.add_sheet('My Sheet')

              dict1 = {"Name": 'Bob' , "Surname": 'red', "Age": 17 }
              headings = ['Name', 'Surname', 'Age']
              for i,heading in enumerate(headings):
              ws.write(0, i, heading)
              ws.write(1, i, str(dict1[heading]))

              wb.save('example.xls')





              share|improve this answer




























                0














                If you want to guarantee the heading order (for versions of Python before 3.7), you'll need to specify it in a list. You can use the xlwt library:



                import xlwt
                from datetime import datetime

                wb = xlwt.Workbook()
                ws = wb.add_sheet('My Sheet')

                dict1 = {"Name": 'Bob' , "Surname": 'red', "Age": 17 }
                headings = ['Name', 'Surname', 'Age']
                for i,heading in enumerate(headings):
                ws.write(0, i, heading)
                ws.write(1, i, str(dict1[heading]))

                wb.save('example.xls')





                share|improve this answer


























                  0












                  0








                  0







                  If you want to guarantee the heading order (for versions of Python before 3.7), you'll need to specify it in a list. You can use the xlwt library:



                  import xlwt
                  from datetime import datetime

                  wb = xlwt.Workbook()
                  ws = wb.add_sheet('My Sheet')

                  dict1 = {"Name": 'Bob' , "Surname": 'red', "Age": 17 }
                  headings = ['Name', 'Surname', 'Age']
                  for i,heading in enumerate(headings):
                  ws.write(0, i, heading)
                  ws.write(1, i, str(dict1[heading]))

                  wb.save('example.xls')





                  share|improve this answer













                  If you want to guarantee the heading order (for versions of Python before 3.7), you'll need to specify it in a list. You can use the xlwt library:



                  import xlwt
                  from datetime import datetime

                  wb = xlwt.Workbook()
                  ws = wb.add_sheet('My Sheet')

                  dict1 = {"Name": 'Bob' , "Surname": 'red', "Age": 17 }
                  headings = ['Name', 'Surname', 'Age']
                  for i,heading in enumerate(headings):
                  ws.write(0, i, heading)
                  ws.write(1, i, str(dict1[heading]))

                  wb.save('example.xls')






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 21 '18 at 18:53









                  xnxxnx

                  15.5k43773




                  15.5k43773























                      0














                      dict1 = {"Name": 'Bob' , "Surname": 'red', "Age": 17 }


                      You can save it in excel by saving it as pandas data frame in 2 different ways



                      Long Format



                      Converting dictionary to pandas dataframe



                      df=pd.DataFrame(list(dict1.items())) #Python 3
                      df=pd.DataFrame(dict1.items()) #Python 2


                      this will store your data as :



                               0    1
                      0 Name Bob
                      1 Surname red
                      2 Age 17


                      Wide Format



                      Converting dictionary key as columns in pandas



                      df=pd.DataFrame(dict1, index=[0])


                      This will convert your data in the desired format



                         Age Name Surname
                      0 17 Bob red


                      And finally, you can save your data in excel



                      df.to_csv('myfile.csv', index =False)





                      share|improve this answer
























                      • If it was helpful, then please accept and upvote the answer. Thanks

                        – Ankur Gulati
                        Nov 21 '18 at 19:15
















                      0














                      dict1 = {"Name": 'Bob' , "Surname": 'red', "Age": 17 }


                      You can save it in excel by saving it as pandas data frame in 2 different ways



                      Long Format



                      Converting dictionary to pandas dataframe



                      df=pd.DataFrame(list(dict1.items())) #Python 3
                      df=pd.DataFrame(dict1.items()) #Python 2


                      this will store your data as :



                               0    1
                      0 Name Bob
                      1 Surname red
                      2 Age 17


                      Wide Format



                      Converting dictionary key as columns in pandas



                      df=pd.DataFrame(dict1, index=[0])


                      This will convert your data in the desired format



                         Age Name Surname
                      0 17 Bob red


                      And finally, you can save your data in excel



                      df.to_csv('myfile.csv', index =False)





                      share|improve this answer
























                      • If it was helpful, then please accept and upvote the answer. Thanks

                        – Ankur Gulati
                        Nov 21 '18 at 19:15














                      0












                      0








                      0







                      dict1 = {"Name": 'Bob' , "Surname": 'red', "Age": 17 }


                      You can save it in excel by saving it as pandas data frame in 2 different ways



                      Long Format



                      Converting dictionary to pandas dataframe



                      df=pd.DataFrame(list(dict1.items())) #Python 3
                      df=pd.DataFrame(dict1.items()) #Python 2


                      this will store your data as :



                               0    1
                      0 Name Bob
                      1 Surname red
                      2 Age 17


                      Wide Format



                      Converting dictionary key as columns in pandas



                      df=pd.DataFrame(dict1, index=[0])


                      This will convert your data in the desired format



                         Age Name Surname
                      0 17 Bob red


                      And finally, you can save your data in excel



                      df.to_csv('myfile.csv', index =False)





                      share|improve this answer













                      dict1 = {"Name": 'Bob' , "Surname": 'red', "Age": 17 }


                      You can save it in excel by saving it as pandas data frame in 2 different ways



                      Long Format



                      Converting dictionary to pandas dataframe



                      df=pd.DataFrame(list(dict1.items())) #Python 3
                      df=pd.DataFrame(dict1.items()) #Python 2


                      this will store your data as :



                               0    1
                      0 Name Bob
                      1 Surname red
                      2 Age 17


                      Wide Format



                      Converting dictionary key as columns in pandas



                      df=pd.DataFrame(dict1, index=[0])


                      This will convert your data in the desired format



                         Age Name Surname
                      0 17 Bob red


                      And finally, you can save your data in excel



                      df.to_csv('myfile.csv', index =False)






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 21 '18 at 19:14









                      Ankur GulatiAnkur Gulati

                      14010




                      14010













                      • If it was helpful, then please accept and upvote the answer. Thanks

                        – Ankur Gulati
                        Nov 21 '18 at 19:15



















                      • If it was helpful, then please accept and upvote the answer. Thanks

                        – Ankur Gulati
                        Nov 21 '18 at 19:15

















                      If it was helpful, then please accept and upvote the answer. Thanks

                      – Ankur Gulati
                      Nov 21 '18 at 19:15





                      If it was helpful, then please accept and upvote the answer. Thanks

                      – Ankur Gulati
                      Nov 21 '18 at 19:15


















                      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%2f53418622%2fpython-copy-dictionary-into-excel%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