Save structured numpy array using np.savetxt with header










1















I have a structure array in the form of



output = np.zeros(names.size, dtype=[('name', 'U32'), ('r', float),('m',float)])


Then I tried to save it into a csv file using np.savetxt. I am wondering if there is way I could also save the label of each column as the header of the csv file?



Thank you in advance.










share|improve this question






















  • output.dtype.names is a list of those field names. You could use that format a header line, e.g. ' '.join(output.dtype.names)

    – hpaulj
    Jun 25 '18 at 16:12











  • @hpaulj Wouldn't that just gives me three column where all my data will appear in the first column?

    – somebodyzh
    Jun 26 '18 at 16:08












  • The header line doesn't affect the layout of the data. That's handled by the fmt as specified in the answer. To savetxt, the header is just a string it writes along with the comment character.

    – hpaulj
    Jun 26 '18 at 16:18















1















I have a structure array in the form of



output = np.zeros(names.size, dtype=[('name', 'U32'), ('r', float),('m',float)])


Then I tried to save it into a csv file using np.savetxt. I am wondering if there is way I could also save the label of each column as the header of the csv file?



Thank you in advance.










share|improve this question






















  • output.dtype.names is a list of those field names. You could use that format a header line, e.g. ' '.join(output.dtype.names)

    – hpaulj
    Jun 25 '18 at 16:12











  • @hpaulj Wouldn't that just gives me three column where all my data will appear in the first column?

    – somebodyzh
    Jun 26 '18 at 16:08












  • The header line doesn't affect the layout of the data. That's handled by the fmt as specified in the answer. To savetxt, the header is just a string it writes along with the comment character.

    – hpaulj
    Jun 26 '18 at 16:18













1












1








1








I have a structure array in the form of



output = np.zeros(names.size, dtype=[('name', 'U32'), ('r', float),('m',float)])


Then I tried to save it into a csv file using np.savetxt. I am wondering if there is way I could also save the label of each column as the header of the csv file?



Thank you in advance.










share|improve this question














I have a structure array in the form of



output = np.zeros(names.size, dtype=[('name', 'U32'), ('r', float),('m',float)])


Then I tried to save it into a csv file using np.savetxt. I am wondering if there is way I could also save the label of each column as the header of the csv file?



Thank you in advance.







python numpy save structured-array






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jun 25 '18 at 15:51









somebodyzhsomebodyzh

61




61












  • output.dtype.names is a list of those field names. You could use that format a header line, e.g. ' '.join(output.dtype.names)

    – hpaulj
    Jun 25 '18 at 16:12











  • @hpaulj Wouldn't that just gives me three column where all my data will appear in the first column?

    – somebodyzh
    Jun 26 '18 at 16:08












  • The header line doesn't affect the layout of the data. That's handled by the fmt as specified in the answer. To savetxt, the header is just a string it writes along with the comment character.

    – hpaulj
    Jun 26 '18 at 16:18

















  • output.dtype.names is a list of those field names. You could use that format a header line, e.g. ' '.join(output.dtype.names)

    – hpaulj
    Jun 25 '18 at 16:12











  • @hpaulj Wouldn't that just gives me three column where all my data will appear in the first column?

    – somebodyzh
    Jun 26 '18 at 16:08












  • The header line doesn't affect the layout of the data. That's handled by the fmt as specified in the answer. To savetxt, the header is just a string it writes along with the comment character.

    – hpaulj
    Jun 26 '18 at 16:18
















output.dtype.names is a list of those field names. You could use that format a header line, e.g. ' '.join(output.dtype.names)

– hpaulj
Jun 25 '18 at 16:12





output.dtype.names is a list of those field names. You could use that format a header line, e.g. ' '.join(output.dtype.names)

– hpaulj
Jun 25 '18 at 16:12













@hpaulj Wouldn't that just gives me three column where all my data will appear in the first column?

– somebodyzh
Jun 26 '18 at 16:08






@hpaulj Wouldn't that just gives me three column where all my data will appear in the first column?

– somebodyzh
Jun 26 '18 at 16:08














The header line doesn't affect the layout of the data. That's handled by the fmt as specified in the answer. To savetxt, the header is just a string it writes along with the comment character.

– hpaulj
Jun 26 '18 at 16:18





The header line doesn't affect the layout of the data. That's handled by the fmt as specified in the answer. To savetxt, the header is just a string it writes along with the comment character.

– hpaulj
Jun 26 '18 at 16:18












2 Answers
2






active

oldest

votes


















0














Below is sample code:



output = np.zeros(names.size, dtype=[('name', 'U32'), ('r', float),('m',float)])
np.savetxt("foo.csv", output, delimiter=",", header="name,r,m", fmt="%s,%f,%f", comments='')


As documented here.






share|improve this answer























  • It gives me 3 columns this way, but all the data appears to be in the first column. I am guessing it is because structured array has each element in the form of 'name float float'. Is there a way to seperate each element into 3 columns?

    – somebodyzh
    Jun 26 '18 at 16:04


















0














You could try a solution similar to this SO answer to pivot the data



dtypes = [('name', 'U32'), ('r', float),('m',float)]
a = np.zeros(5, dtype=dtypes)
b = numpy.vstack(map(list, a))


Where you map list over the recarray tuples, and then vertically stack them.



Then you can do the following...



names = [n for n, t in dtypes]
numpy.savetxt('test.csv', b, header=','.join(names), fmt=','.join(['%s']*b.shape[1]))





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51027447%2fsave-structured-numpy-array-using-np-savetxt-with-header%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














    Below is sample code:



    output = np.zeros(names.size, dtype=[('name', 'U32'), ('r', float),('m',float)])
    np.savetxt("foo.csv", output, delimiter=",", header="name,r,m", fmt="%s,%f,%f", comments='')


    As documented here.






    share|improve this answer























    • It gives me 3 columns this way, but all the data appears to be in the first column. I am guessing it is because structured array has each element in the form of 'name float float'. Is there a way to seperate each element into 3 columns?

      – somebodyzh
      Jun 26 '18 at 16:04















    0














    Below is sample code:



    output = np.zeros(names.size, dtype=[('name', 'U32'), ('r', float),('m',float)])
    np.savetxt("foo.csv", output, delimiter=",", header="name,r,m", fmt="%s,%f,%f", comments='')


    As documented here.






    share|improve this answer























    • It gives me 3 columns this way, but all the data appears to be in the first column. I am guessing it is because structured array has each element in the form of 'name float float'. Is there a way to seperate each element into 3 columns?

      – somebodyzh
      Jun 26 '18 at 16:04













    0












    0








    0







    Below is sample code:



    output = np.zeros(names.size, dtype=[('name', 'U32'), ('r', float),('m',float)])
    np.savetxt("foo.csv", output, delimiter=",", header="name,r,m", fmt="%s,%f,%f", comments='')


    As documented here.






    share|improve this answer













    Below is sample code:



    output = np.zeros(names.size, dtype=[('name', 'U32'), ('r', float),('m',float)])
    np.savetxt("foo.csv", output, delimiter=",", header="name,r,m", fmt="%s,%f,%f", comments='')


    As documented here.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jun 25 '18 at 16:03









    chifu linchifu lin

    665




    665












    • It gives me 3 columns this way, but all the data appears to be in the first column. I am guessing it is because structured array has each element in the form of 'name float float'. Is there a way to seperate each element into 3 columns?

      – somebodyzh
      Jun 26 '18 at 16:04

















    • It gives me 3 columns this way, but all the data appears to be in the first column. I am guessing it is because structured array has each element in the form of 'name float float'. Is there a way to seperate each element into 3 columns?

      – somebodyzh
      Jun 26 '18 at 16:04
















    It gives me 3 columns this way, but all the data appears to be in the first column. I am guessing it is because structured array has each element in the form of 'name float float'. Is there a way to seperate each element into 3 columns?

    – somebodyzh
    Jun 26 '18 at 16:04





    It gives me 3 columns this way, but all the data appears to be in the first column. I am guessing it is because structured array has each element in the form of 'name float float'. Is there a way to seperate each element into 3 columns?

    – somebodyzh
    Jun 26 '18 at 16:04













    0














    You could try a solution similar to this SO answer to pivot the data



    dtypes = [('name', 'U32'), ('r', float),('m',float)]
    a = np.zeros(5, dtype=dtypes)
    b = numpy.vstack(map(list, a))


    Where you map list over the recarray tuples, and then vertically stack them.



    Then you can do the following...



    names = [n for n, t in dtypes]
    numpy.savetxt('test.csv', b, header=','.join(names), fmt=','.join(['%s']*b.shape[1]))





    share|improve this answer





























      0














      You could try a solution similar to this SO answer to pivot the data



      dtypes = [('name', 'U32'), ('r', float),('m',float)]
      a = np.zeros(5, dtype=dtypes)
      b = numpy.vstack(map(list, a))


      Where you map list over the recarray tuples, and then vertically stack them.



      Then you can do the following...



      names = [n for n, t in dtypes]
      numpy.savetxt('test.csv', b, header=','.join(names), fmt=','.join(['%s']*b.shape[1]))





      share|improve this answer



























        0












        0








        0







        You could try a solution similar to this SO answer to pivot the data



        dtypes = [('name', 'U32'), ('r', float),('m',float)]
        a = np.zeros(5, dtype=dtypes)
        b = numpy.vstack(map(list, a))


        Where you map list over the recarray tuples, and then vertically stack them.



        Then you can do the following...



        names = [n for n, t in dtypes]
        numpy.savetxt('test.csv', b, header=','.join(names), fmt=','.join(['%s']*b.shape[1]))





        share|improve this answer















        You could try a solution similar to this SO answer to pivot the data



        dtypes = [('name', 'U32'), ('r', float),('m',float)]
        a = np.zeros(5, dtype=dtypes)
        b = numpy.vstack(map(list, a))


        Where you map list over the recarray tuples, and then vertically stack them.



        Then you can do the following...



        names = [n for n, t in dtypes]
        numpy.savetxt('test.csv', b, header=','.join(names), fmt=','.join(['%s']*b.shape[1]))






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 14 '18 at 8:57

























        answered Nov 13 '18 at 11:52









        ryanjdillonryanjdillon

        7,04864974




        7,04864974



























            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%2f51027447%2fsave-structured-numpy-array-using-np-savetxt-with-header%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

            Kleinkühnau

            Makov (Slowakei)

            Deutsches Schauspielhaus