Pandas.read_excel: Blanks in string columns convert to floats, converting via str() produces string 'Nan'

Multi tool use
Multi tool use












0














In importing an excel file via read_excel, string columns containing blanks are read in as float. When I force them back to strings via str(), the blanks become the string 'Nan', such that when I specify df[df.column=='Nan'], the values are returned.



How can I revise my code to ensure that blanks within a column do not force the entire column to float, and that blanks in a string column are read in as ''?










share|improve this question






















  • you can replace the Nan with ''
    – meW
    Dec 28 '18 at 3:16










  • but what if the string could actually be Nan? Example: last name column; last name is Nan.
    – UBHDNNX
    Dec 28 '18 at 3:18










  • provide a sample data in your question
    – meW
    Dec 28 '18 at 3:24






  • 1




    Try: df = pd.read_excel(...).fillna('')
    – James
    Dec 28 '18 at 3:27










  • @James that worked perfectly. Push it to an answer and I'll mark it correct.
    – UBHDNNX
    Dec 28 '18 at 3:39
















0














In importing an excel file via read_excel, string columns containing blanks are read in as float. When I force them back to strings via str(), the blanks become the string 'Nan', such that when I specify df[df.column=='Nan'], the values are returned.



How can I revise my code to ensure that blanks within a column do not force the entire column to float, and that blanks in a string column are read in as ''?










share|improve this question






















  • you can replace the Nan with ''
    – meW
    Dec 28 '18 at 3:16










  • but what if the string could actually be Nan? Example: last name column; last name is Nan.
    – UBHDNNX
    Dec 28 '18 at 3:18










  • provide a sample data in your question
    – meW
    Dec 28 '18 at 3:24






  • 1




    Try: df = pd.read_excel(...).fillna('')
    – James
    Dec 28 '18 at 3:27










  • @James that worked perfectly. Push it to an answer and I'll mark it correct.
    – UBHDNNX
    Dec 28 '18 at 3:39














0












0








0







In importing an excel file via read_excel, string columns containing blanks are read in as float. When I force them back to strings via str(), the blanks become the string 'Nan', such that when I specify df[df.column=='Nan'], the values are returned.



How can I revise my code to ensure that blanks within a column do not force the entire column to float, and that blanks in a string column are read in as ''?










share|improve this question













In importing an excel file via read_excel, string columns containing blanks are read in as float. When I force them back to strings via str(), the blanks become the string 'Nan', such that when I specify df[df.column=='Nan'], the values are returned.



How can I revise my code to ensure that blanks within a column do not force the entire column to float, and that blanks in a string column are read in as ''?







python pandas nan






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 28 '18 at 3:14









UBHDNNX

53




53












  • you can replace the Nan with ''
    – meW
    Dec 28 '18 at 3:16










  • but what if the string could actually be Nan? Example: last name column; last name is Nan.
    – UBHDNNX
    Dec 28 '18 at 3:18










  • provide a sample data in your question
    – meW
    Dec 28 '18 at 3:24






  • 1




    Try: df = pd.read_excel(...).fillna('')
    – James
    Dec 28 '18 at 3:27










  • @James that worked perfectly. Push it to an answer and I'll mark it correct.
    – UBHDNNX
    Dec 28 '18 at 3:39


















  • you can replace the Nan with ''
    – meW
    Dec 28 '18 at 3:16










  • but what if the string could actually be Nan? Example: last name column; last name is Nan.
    – UBHDNNX
    Dec 28 '18 at 3:18










  • provide a sample data in your question
    – meW
    Dec 28 '18 at 3:24






  • 1




    Try: df = pd.read_excel(...).fillna('')
    – James
    Dec 28 '18 at 3:27










  • @James that worked perfectly. Push it to an answer and I'll mark it correct.
    – UBHDNNX
    Dec 28 '18 at 3:39
















you can replace the Nan with ''
– meW
Dec 28 '18 at 3:16




you can replace the Nan with ''
– meW
Dec 28 '18 at 3:16












but what if the string could actually be Nan? Example: last name column; last name is Nan.
– UBHDNNX
Dec 28 '18 at 3:18




but what if the string could actually be Nan? Example: last name column; last name is Nan.
– UBHDNNX
Dec 28 '18 at 3:18












provide a sample data in your question
– meW
Dec 28 '18 at 3:24




provide a sample data in your question
– meW
Dec 28 '18 at 3:24




1




1




Try: df = pd.read_excel(...).fillna('')
– James
Dec 28 '18 at 3:27




Try: df = pd.read_excel(...).fillna('')
– James
Dec 28 '18 at 3:27












@James that worked perfectly. Push it to an answer and I'll mark it correct.
– UBHDNNX
Dec 28 '18 at 3:39




@James that worked perfectly. Push it to an answer and I'll mark it correct.
– UBHDNNX
Dec 28 '18 at 3:39












2 Answers
2






active

oldest

votes


















1














Try filling NA values on read:



df = pd.read_excel(...).fillna('')





share|improve this answer





























    0














    You don't actually have to convert to string. You can actually do a:



    df[df.colname!=df.colname] #Returns True if it encounters a NaN





    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%2f53953286%2fpandas-read-excel-blanks-in-string-columns-convert-to-floats-converting-via-st%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














      Try filling NA values on read:



      df = pd.read_excel(...).fillna('')





      share|improve this answer


























        1














        Try filling NA values on read:



        df = pd.read_excel(...).fillna('')





        share|improve this answer
























          1












          1








          1






          Try filling NA values on read:



          df = pd.read_excel(...).fillna('')





          share|improve this answer












          Try filling NA values on read:



          df = pd.read_excel(...).fillna('')






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 28 '18 at 21:43









          James

          13.1k11532




          13.1k11532

























              0














              You don't actually have to convert to string. You can actually do a:



              df[df.colname!=df.colname] #Returns True if it encounters a NaN





              share|improve this answer


























                0














                You don't actually have to convert to string. You can actually do a:



                df[df.colname!=df.colname] #Returns True if it encounters a NaN





                share|improve this answer
























                  0












                  0








                  0






                  You don't actually have to convert to string. You can actually do a:



                  df[df.colname!=df.colname] #Returns True if it encounters a NaN





                  share|improve this answer












                  You don't actually have to convert to string. You can actually do a:



                  df[df.colname!=df.colname] #Returns True if it encounters a NaN






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 28 '18 at 3:50









                  ycx

                  1,13616




                  1,13616






























                      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%2f53953286%2fpandas-read-excel-blanks-in-string-columns-convert-to-floats-converting-via-st%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







                      U,ZdtvvD6ngx7hVZxItLiFSRwx,WMQ05B 710w5PiQoqhm,Fa6vsk i x3XoVL2 m8D,lWG,frP,rRbOb,nW,OaAGVk6UEUsdrcW7w9eeO
                      nh6QK3cphZjcN6Y3p6 9vfTkue bm0YfTOJBmj1,DcQilLk66aVPS

                      Popular posts from this blog

                      Monofisismo

                      compose and upload a new article using a custom form

                      “attempting to read past stream EOM” using Sybase.AdoNet4.AseClient