How to preserve the format when writing to csv using pandas?












0














I have a text file like this:



id,name,sex,
1,Sam,M,
2,Ann,F,
3,Peter,
4,Ben,M,


Then, I read the file:
df = pd.read_csv('data.csv')



After that, I write it to another file:
df.to_csv('new_data.csv', index = False)



Then, I get



id,name,sex,Unnamed: 3
1,Sam,M,
2,Ann,F,
3,Peter,,
4,Ben,M,


You see that there are two commas instead of one in the fourth line.



How to preserve the format when using pd.to_csv?










share|improve this question






















  • That's because you have a comma at the end.
    – Mohit Motwani
    Nov 20 '18 at 11:30
















0














I have a text file like this:



id,name,sex,
1,Sam,M,
2,Ann,F,
3,Peter,
4,Ben,M,


Then, I read the file:
df = pd.read_csv('data.csv')



After that, I write it to another file:
df.to_csv('new_data.csv', index = False)



Then, I get



id,name,sex,Unnamed: 3
1,Sam,M,
2,Ann,F,
3,Peter,,
4,Ben,M,


You see that there are two commas instead of one in the fourth line.



How to preserve the format when using pd.to_csv?










share|improve this question






















  • That's because you have a comma at the end.
    – Mohit Motwani
    Nov 20 '18 at 11:30














0












0








0







I have a text file like this:



id,name,sex,
1,Sam,M,
2,Ann,F,
3,Peter,
4,Ben,M,


Then, I read the file:
df = pd.read_csv('data.csv')



After that, I write it to another file:
df.to_csv('new_data.csv', index = False)



Then, I get



id,name,sex,Unnamed: 3
1,Sam,M,
2,Ann,F,
3,Peter,,
4,Ben,M,


You see that there are two commas instead of one in the fourth line.



How to preserve the format when using pd.to_csv?










share|improve this question













I have a text file like this:



id,name,sex,
1,Sam,M,
2,Ann,F,
3,Peter,
4,Ben,M,


Then, I read the file:
df = pd.read_csv('data.csv')



After that, I write it to another file:
df.to_csv('new_data.csv', index = False)



Then, I get



id,name,sex,Unnamed: 3
1,Sam,M,
2,Ann,F,
3,Peter,,
4,Ben,M,


You see that there are two commas instead of one in the fourth line.



How to preserve the format when using pd.to_csv?







python pandas






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 11:24









Chan

387214




387214












  • That's because you have a comma at the end.
    – Mohit Motwani
    Nov 20 '18 at 11:30


















  • That's because you have a comma at the end.
    – Mohit Motwani
    Nov 20 '18 at 11:30
















That's because you have a comma at the end.
– Mohit Motwani
Nov 20 '18 at 11:30




That's because you have a comma at the end.
– Mohit Motwani
Nov 20 '18 at 11:30












2 Answers
2






active

oldest

votes


















1














pandas is preserving the format - the 3d row has no sex, and as such the csv should have an empty column - that is why you get to commas, since you are separating an empty column.



Your original text file was not a valid csv file.



What you want to do is something else, which is not write a valid csv file - you will have to do this yourself, I do not know of any existing method to create your format.






share|improve this answer





























    1














    The problem in your code is that you have a comma after the sex column in your file. So read_csv thinks it's a new column, which has no name and data.



    df= pd.read_csv('data.csv')
    df

    id name sex Unnamed: 3
    0 1 Sam M NaN
    1 2 Ann F NaN
    2 3 Peter NaN NaN
    3 4 Ben M NaN


    Hence you have an extra Unnamed column. So when you write the to_csv, it adds two empty values in the 3rd row and hence why, two ,.



    Try:



    df = pd.read_csv('data.csv', use_cols = ['id', 'name', 'sex'])
    df.to_csv('new_data.csv', index = False)





    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%2f53391976%2fhow-to-preserve-the-format-when-writing-to-csv-using-pandas%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









      1














      pandas is preserving the format - the 3d row has no sex, and as such the csv should have an empty column - that is why you get to commas, since you are separating an empty column.



      Your original text file was not a valid csv file.



      What you want to do is something else, which is not write a valid csv file - you will have to do this yourself, I do not know of any existing method to create your format.






      share|improve this answer


























        1














        pandas is preserving the format - the 3d row has no sex, and as such the csv should have an empty column - that is why you get to commas, since you are separating an empty column.



        Your original text file was not a valid csv file.



        What you want to do is something else, which is not write a valid csv file - you will have to do this yourself, I do not know of any existing method to create your format.






        share|improve this answer
























          1












          1








          1






          pandas is preserving the format - the 3d row has no sex, and as such the csv should have an empty column - that is why you get to commas, since you are separating an empty column.



          Your original text file was not a valid csv file.



          What you want to do is something else, which is not write a valid csv file - you will have to do this yourself, I do not know of any existing method to create your format.






          share|improve this answer












          pandas is preserving the format - the 3d row has no sex, and as such the csv should have an empty column - that is why you get to commas, since you are separating an empty column.



          Your original text file was not a valid csv file.



          What you want to do is something else, which is not write a valid csv file - you will have to do this yourself, I do not know of any existing method to create your format.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 11:29









          kabanus

          11.3k31338




          11.3k31338

























              1














              The problem in your code is that you have a comma after the sex column in your file. So read_csv thinks it's a new column, which has no name and data.



              df= pd.read_csv('data.csv')
              df

              id name sex Unnamed: 3
              0 1 Sam M NaN
              1 2 Ann F NaN
              2 3 Peter NaN NaN
              3 4 Ben M NaN


              Hence you have an extra Unnamed column. So when you write the to_csv, it adds two empty values in the 3rd row and hence why, two ,.



              Try:



              df = pd.read_csv('data.csv', use_cols = ['id', 'name', 'sex'])
              df.to_csv('new_data.csv', index = False)





              share|improve this answer




























                1














                The problem in your code is that you have a comma after the sex column in your file. So read_csv thinks it's a new column, which has no name and data.



                df= pd.read_csv('data.csv')
                df

                id name sex Unnamed: 3
                0 1 Sam M NaN
                1 2 Ann F NaN
                2 3 Peter NaN NaN
                3 4 Ben M NaN


                Hence you have an extra Unnamed column. So when you write the to_csv, it adds two empty values in the 3rd row and hence why, two ,.



                Try:



                df = pd.read_csv('data.csv', use_cols = ['id', 'name', 'sex'])
                df.to_csv('new_data.csv', index = False)





                share|improve this answer


























                  1












                  1








                  1






                  The problem in your code is that you have a comma after the sex column in your file. So read_csv thinks it's a new column, which has no name and data.



                  df= pd.read_csv('data.csv')
                  df

                  id name sex Unnamed: 3
                  0 1 Sam M NaN
                  1 2 Ann F NaN
                  2 3 Peter NaN NaN
                  3 4 Ben M NaN


                  Hence you have an extra Unnamed column. So when you write the to_csv, it adds two empty values in the 3rd row and hence why, two ,.



                  Try:



                  df = pd.read_csv('data.csv', use_cols = ['id', 'name', 'sex'])
                  df.to_csv('new_data.csv', index = False)





                  share|improve this answer














                  The problem in your code is that you have a comma after the sex column in your file. So read_csv thinks it's a new column, which has no name and data.



                  df= pd.read_csv('data.csv')
                  df

                  id name sex Unnamed: 3
                  0 1 Sam M NaN
                  1 2 Ann F NaN
                  2 3 Peter NaN NaN
                  3 4 Ben M NaN


                  Hence you have an extra Unnamed column. So when you write the to_csv, it adds two empty values in the 3rd row and hence why, two ,.



                  Try:



                  df = pd.read_csv('data.csv', use_cols = ['id', 'name', 'sex'])
                  df.to_csv('new_data.csv', index = False)






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 20 '18 at 11:41

























                  answered Nov 20 '18 at 11:34









                  Mohit Motwani

                  1,1131422




                  1,1131422






























                      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.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • 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%2f53391976%2fhow-to-preserve-the-format-when-writing-to-csv-using-pandas%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

                      If I really need a card on my start hand, how many mulligans make sense? [duplicate]

                      Alcedinidae

                      Can an atomic nucleus contain both particles and antiparticles? [duplicate]