Trying to change pandas column dtype from str to float












0












$begingroup$


I am trying to convert a pandas column from a str to a float. Before I convert the strings to floats using astype(float), I need to remove characters from the string which cannot be converted into floats, like a comma or a space. In trying to remove these characters, I am using list compression and the str.replace method like this:



data.loc[:,'column_name'] = [char.replace(',', '') for char in 
data.loc[:,'column_name']]


but I am getting an error saying:



AttributeError: 'float' object has no attribute 'replace'.



The confusing part, though, is that when I look at the dtypes, the dtype for that column shows up as 'o', or object. So I have no idea what is going on.










share|improve this question









New contributor




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







$endgroup$








  • 1




    $begingroup$
    look at set([type(x) for x in data['column_name'].values] - a common problem is that it has some NaN values which are float, not string.
    $endgroup$
    – Valentas
    18 hours ago










  • $begingroup$
    The question is about programming, not about Data Science, it is better if you move your question to StackOverflow.
    $endgroup$
    – omar
    18 hours ago
















0












$begingroup$


I am trying to convert a pandas column from a str to a float. Before I convert the strings to floats using astype(float), I need to remove characters from the string which cannot be converted into floats, like a comma or a space. In trying to remove these characters, I am using list compression and the str.replace method like this:



data.loc[:,'column_name'] = [char.replace(',', '') for char in 
data.loc[:,'column_name']]


but I am getting an error saying:



AttributeError: 'float' object has no attribute 'replace'.



The confusing part, though, is that when I look at the dtypes, the dtype for that column shows up as 'o', or object. So I have no idea what is going on.










share|improve this question









New contributor




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







$endgroup$








  • 1




    $begingroup$
    look at set([type(x) for x in data['column_name'].values] - a common problem is that it has some NaN values which are float, not string.
    $endgroup$
    – Valentas
    18 hours ago










  • $begingroup$
    The question is about programming, not about Data Science, it is better if you move your question to StackOverflow.
    $endgroup$
    – omar
    18 hours ago














0












0








0





$begingroup$


I am trying to convert a pandas column from a str to a float. Before I convert the strings to floats using astype(float), I need to remove characters from the string which cannot be converted into floats, like a comma or a space. In trying to remove these characters, I am using list compression and the str.replace method like this:



data.loc[:,'column_name'] = [char.replace(',', '') for char in 
data.loc[:,'column_name']]


but I am getting an error saying:



AttributeError: 'float' object has no attribute 'replace'.



The confusing part, though, is that when I look at the dtypes, the dtype for that column shows up as 'o', or object. So I have no idea what is going on.










share|improve this question









New contributor




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







$endgroup$




I am trying to convert a pandas column from a str to a float. Before I convert the strings to floats using astype(float), I need to remove characters from the string which cannot be converted into floats, like a comma or a space. In trying to remove these characters, I am using list compression and the str.replace method like this:



data.loc[:,'column_name'] = [char.replace(',', '') for char in 
data.loc[:,'column_name']]


but I am getting an error saying:



AttributeError: 'float' object has no attribute 'replace'.



The confusing part, though, is that when I look at the dtypes, the dtype for that column shows up as 'o', or object. So I have no idea what is going on.







python pandas






share|improve this question









New contributor




Sebastian 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 question









New contributor




Sebastian 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 question




share|improve this question








edited 15 hours ago









Siong Thye Goh

1,122418




1,122418






New contributor




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









asked 19 hours ago









SebastianSebastian

12




12




New contributor




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





New contributor





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






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








  • 1




    $begingroup$
    look at set([type(x) for x in data['column_name'].values] - a common problem is that it has some NaN values which are float, not string.
    $endgroup$
    – Valentas
    18 hours ago










  • $begingroup$
    The question is about programming, not about Data Science, it is better if you move your question to StackOverflow.
    $endgroup$
    – omar
    18 hours ago














  • 1




    $begingroup$
    look at set([type(x) for x in data['column_name'].values] - a common problem is that it has some NaN values which are float, not string.
    $endgroup$
    – Valentas
    18 hours ago










  • $begingroup$
    The question is about programming, not about Data Science, it is better if you move your question to StackOverflow.
    $endgroup$
    – omar
    18 hours ago








1




1




$begingroup$
look at set([type(x) for x in data['column_name'].values] - a common problem is that it has some NaN values which are float, not string.
$endgroup$
– Valentas
18 hours ago




$begingroup$
look at set([type(x) for x in data['column_name'].values] - a common problem is that it has some NaN values which are float, not string.
$endgroup$
– Valentas
18 hours ago












$begingroup$
The question is about programming, not about Data Science, it is better if you move your question to StackOverflow.
$endgroup$
– omar
18 hours ago




$begingroup$
The question is about programming, not about Data Science, it is better if you move your question to StackOverflow.
$endgroup$
– omar
18 hours ago










2 Answers
2






active

oldest

votes


















1












$begingroup$

In pandas the object type is used when there is not a clear distinction between the types stored in the column.



So, I guess that in your column, some objects are float type and some objects are str type. Or maybe, you are also dealing with NaN objects, NaN objects are float objects.



a) Convert the column to string: Are you getting your DataFrame from a CSV or XLS format file? Then at the moment of reading the file, you can specify that that column is an str type or just make the type conversion of the column you are dealing with.



b) After that, you can apply the string changes and/or deal with the NaN objects.



c) Finally, you transform your column into float type`.






share|improve this answer











$endgroup$





















    0












    $begingroup$

    Maybe it's a very rudimentary method but I would just do



    listt = 
    for i in data['column_name']:
    listt.append(float(i))
    data['FloatData'] = listt





    share|improve this answer








    New contributor




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






    $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
      });


      }
      });






      Sebastian is a new contributor. Be nice, and check out our Code of Conduct.










      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f45799%2ftrying-to-change-pandas-column-dtype-from-str-to-float%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












      $begingroup$

      In pandas the object type is used when there is not a clear distinction between the types stored in the column.



      So, I guess that in your column, some objects are float type and some objects are str type. Or maybe, you are also dealing with NaN objects, NaN objects are float objects.



      a) Convert the column to string: Are you getting your DataFrame from a CSV or XLS format file? Then at the moment of reading the file, you can specify that that column is an str type or just make the type conversion of the column you are dealing with.



      b) After that, you can apply the string changes and/or deal with the NaN objects.



      c) Finally, you transform your column into float type`.






      share|improve this answer











      $endgroup$


















        1












        $begingroup$

        In pandas the object type is used when there is not a clear distinction between the types stored in the column.



        So, I guess that in your column, some objects are float type and some objects are str type. Or maybe, you are also dealing with NaN objects, NaN objects are float objects.



        a) Convert the column to string: Are you getting your DataFrame from a CSV or XLS format file? Then at the moment of reading the file, you can specify that that column is an str type or just make the type conversion of the column you are dealing with.



        b) After that, you can apply the string changes and/or deal with the NaN objects.



        c) Finally, you transform your column into float type`.






        share|improve this answer











        $endgroup$
















          1












          1








          1





          $begingroup$

          In pandas the object type is used when there is not a clear distinction between the types stored in the column.



          So, I guess that in your column, some objects are float type and some objects are str type. Or maybe, you are also dealing with NaN objects, NaN objects are float objects.



          a) Convert the column to string: Are you getting your DataFrame from a CSV or XLS format file? Then at the moment of reading the file, you can specify that that column is an str type or just make the type conversion of the column you are dealing with.



          b) After that, you can apply the string changes and/or deal with the NaN objects.



          c) Finally, you transform your column into float type`.






          share|improve this answer











          $endgroup$



          In pandas the object type is used when there is not a clear distinction between the types stored in the column.



          So, I guess that in your column, some objects are float type and some objects are str type. Or maybe, you are also dealing with NaN objects, NaN objects are float objects.



          a) Convert the column to string: Are you getting your DataFrame from a CSV or XLS format file? Then at the moment of reading the file, you can specify that that column is an str type or just make the type conversion of the column you are dealing with.



          b) After that, you can apply the string changes and/or deal with the NaN objects.



          c) Finally, you transform your column into float type`.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 18 hours ago

























          answered 18 hours ago









          omaromar

          16114




          16114























              0












              $begingroup$

              Maybe it's a very rudimentary method but I would just do



              listt = 
              for i in data['column_name']:
              listt.append(float(i))
              data['FloatData'] = listt





              share|improve this answer








              New contributor




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

                Maybe it's a very rudimentary method but I would just do



                listt = 
                for i in data['column_name']:
                listt.append(float(i))
                data['FloatData'] = listt





                share|improve this answer








                New contributor




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






                $endgroup$
















                  0












                  0








                  0





                  $begingroup$

                  Maybe it's a very rudimentary method but I would just do



                  listt = 
                  for i in data['column_name']:
                  listt.append(float(i))
                  data['FloatData'] = listt





                  share|improve this answer








                  New contributor




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






                  $endgroup$



                  Maybe it's a very rudimentary method but I would just do



                  listt = 
                  for i in data['column_name']:
                  listt.append(float(i))
                  data['FloatData'] = listt






                  share|improve this answer








                  New contributor




                  Armando Delgado 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




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









                  answered 15 hours ago









                  Armando DelgadoArmando Delgado

                  1




                  1




                  New contributor




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





                  New contributor





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






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






















                      Sebastian is a new contributor. Be nice, and check out our Code of Conduct.










                      draft saved

                      draft discarded


















                      Sebastian is a new contributor. Be nice, and check out our Code of Conduct.













                      Sebastian is a new contributor. Be nice, and check out our Code of Conduct.












                      Sebastian is a new contributor. Be nice, and check out our Code of Conduct.
















                      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%2f45799%2ftrying-to-change-pandas-column-dtype-from-str-to-float%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

                      How to label and detect the document text images

                      Vallis Paradisi

                      Tabula Rosettana