How to remove rows in a Pandas Dataframe with a specific column containing numbers only?











up vote
3
down vote

favorite












Lets say I have this DF:



ID     IGName          Date_created
0 BananaMan 09/10/2018
1 Superman247 10/10/2009
2 123456789 08/03/2011
3 Nameless101 07/12/2012


I want to be able to remove all the rows in the DF where the IGName is only numbers.



Like how in this example, row 3 is all numbers. I want to be able to keep the names alphanumeric rows but not the rows with ONLY numerics.



I want the result to look like this:



ID     IGName          Date_created
0 BananaMan 09/10/2018
1 Superman247 10/10/2009
3 Nameless101 07/12/2012









share|improve this question


























    up vote
    3
    down vote

    favorite












    Lets say I have this DF:



    ID     IGName          Date_created
    0 BananaMan 09/10/2018
    1 Superman247 10/10/2009
    2 123456789 08/03/2011
    3 Nameless101 07/12/2012


    I want to be able to remove all the rows in the DF where the IGName is only numbers.



    Like how in this example, row 3 is all numbers. I want to be able to keep the names alphanumeric rows but not the rows with ONLY numerics.



    I want the result to look like this:



    ID     IGName          Date_created
    0 BananaMan 09/10/2018
    1 Superman247 10/10/2009
    3 Nameless101 07/12/2012









    share|improve this question
























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      Lets say I have this DF:



      ID     IGName          Date_created
      0 BananaMan 09/10/2018
      1 Superman247 10/10/2009
      2 123456789 08/03/2011
      3 Nameless101 07/12/2012


      I want to be able to remove all the rows in the DF where the IGName is only numbers.



      Like how in this example, row 3 is all numbers. I want to be able to keep the names alphanumeric rows but not the rows with ONLY numerics.



      I want the result to look like this:



      ID     IGName          Date_created
      0 BananaMan 09/10/2018
      1 Superman247 10/10/2009
      3 Nameless101 07/12/2012









      share|improve this question













      Lets say I have this DF:



      ID     IGName          Date_created
      0 BananaMan 09/10/2018
      1 Superman247 10/10/2009
      2 123456789 08/03/2011
      3 Nameless101 07/12/2012


      I want to be able to remove all the rows in the DF where the IGName is only numbers.



      Like how in this example, row 3 is all numbers. I want to be able to keep the names alphanumeric rows but not the rows with ONLY numerics.



      I want the result to look like this:



      ID     IGName          Date_created
      0 BananaMan 09/10/2018
      1 Superman247 10/10/2009
      3 Nameless101 07/12/2012






      python pandas






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 at 1:16









      The Dodo

      738




      738
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          You could do:



          import pandas as pd


          data = [[0, 'BananaMan', '09/10/2018'],
          [1, 'Superman247', '10/10/2009'],
          [2, '123456789', '08/03/2011'],
          [3, 'Nameless101', '07/12/2012']]

          df = pd.DataFrame(data=data, columns=['ID', 'IGName', 'Date_created'])

          df = df[~df['IGName'].str.isnumeric()]

          print(df)


          Output



             ID       IGName Date_created
          0 0 BananaMan 09/10/2018
          1 1 Superman247 10/10/2009
          3 3 Nameless101 07/12/2012


          From the documentation:




          Check whether all characters in each string in the Series/Index are
          numeric. Equivalent to str.isnumeric().




          Note that this solution assumes the column 'IGName' is of type string, otherwise you need to cast it to string, doing something like (as mentioned by @RafaelC):



          df['IGName'] = df['IGName'].astype(str)





          share|improve this answer























          • This will fail if numbers are actually numbers (int or float type).
            – RafaelC
            Nov 20 at 1:29












          • @RafaelC do you mean if the column is of type int or float?
            – Daniel Mesejo
            Nov 20 at 1:31










          • I mean if you have mixed types in the same column. For example, if in your data, you had [2, 123456789, '08/03/2011'] instead of [2, '123456789', '08/03/2011']
            – RafaelC
            Nov 20 at 1:33










          • Does it suffices to cast the column to string first?
            – Daniel Mesejo
            Nov 20 at 1:34










          • As a matter of fact, yes ;}. Use .astype(str)
            – RafaelC
            Nov 20 at 1:35




















          up vote
          0
          down vote













          Use df[...]:



          print(df[~df['IGName'].str.isnumeric()])


          Or:



          print(df[df['IGName'].str.contains(r'D+')])


          Both Output:



             ID       IGName Date_created
          0 0 BananaMan 09/10/2018
          1 1 Superman247 10/10/2009
          3 3 Nameless101 07/12/2012


          If IGName has integers do:



          print(df[pd.to_numeric(df.IGName, errors='coerce').notnull()])





          share|improve this answer



















          • 1




            Same observation from above applies here
            – RafaelC
            Nov 20 at 1:36










          • @RafaelC Now is it good.
            – U9-Forward
            Nov 20 at 1:45











          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',
          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%2f53384890%2fhow-to-remove-rows-in-a-pandas-dataframe-with-a-specific-column-containing-numbe%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








          up vote
          4
          down vote



          accepted










          You could do:



          import pandas as pd


          data = [[0, 'BananaMan', '09/10/2018'],
          [1, 'Superman247', '10/10/2009'],
          [2, '123456789', '08/03/2011'],
          [3, 'Nameless101', '07/12/2012']]

          df = pd.DataFrame(data=data, columns=['ID', 'IGName', 'Date_created'])

          df = df[~df['IGName'].str.isnumeric()]

          print(df)


          Output



             ID       IGName Date_created
          0 0 BananaMan 09/10/2018
          1 1 Superman247 10/10/2009
          3 3 Nameless101 07/12/2012


          From the documentation:




          Check whether all characters in each string in the Series/Index are
          numeric. Equivalent to str.isnumeric().




          Note that this solution assumes the column 'IGName' is of type string, otherwise you need to cast it to string, doing something like (as mentioned by @RafaelC):



          df['IGName'] = df['IGName'].astype(str)





          share|improve this answer























          • This will fail if numbers are actually numbers (int or float type).
            – RafaelC
            Nov 20 at 1:29












          • @RafaelC do you mean if the column is of type int or float?
            – Daniel Mesejo
            Nov 20 at 1:31










          • I mean if you have mixed types in the same column. For example, if in your data, you had [2, 123456789, '08/03/2011'] instead of [2, '123456789', '08/03/2011']
            – RafaelC
            Nov 20 at 1:33










          • Does it suffices to cast the column to string first?
            – Daniel Mesejo
            Nov 20 at 1:34










          • As a matter of fact, yes ;}. Use .astype(str)
            – RafaelC
            Nov 20 at 1:35

















          up vote
          4
          down vote



          accepted










          You could do:



          import pandas as pd


          data = [[0, 'BananaMan', '09/10/2018'],
          [1, 'Superman247', '10/10/2009'],
          [2, '123456789', '08/03/2011'],
          [3, 'Nameless101', '07/12/2012']]

          df = pd.DataFrame(data=data, columns=['ID', 'IGName', 'Date_created'])

          df = df[~df['IGName'].str.isnumeric()]

          print(df)


          Output



             ID       IGName Date_created
          0 0 BananaMan 09/10/2018
          1 1 Superman247 10/10/2009
          3 3 Nameless101 07/12/2012


          From the documentation:




          Check whether all characters in each string in the Series/Index are
          numeric. Equivalent to str.isnumeric().




          Note that this solution assumes the column 'IGName' is of type string, otherwise you need to cast it to string, doing something like (as mentioned by @RafaelC):



          df['IGName'] = df['IGName'].astype(str)





          share|improve this answer























          • This will fail if numbers are actually numbers (int or float type).
            – RafaelC
            Nov 20 at 1:29












          • @RafaelC do you mean if the column is of type int or float?
            – Daniel Mesejo
            Nov 20 at 1:31










          • I mean if you have mixed types in the same column. For example, if in your data, you had [2, 123456789, '08/03/2011'] instead of [2, '123456789', '08/03/2011']
            – RafaelC
            Nov 20 at 1:33










          • Does it suffices to cast the column to string first?
            – Daniel Mesejo
            Nov 20 at 1:34










          • As a matter of fact, yes ;}. Use .astype(str)
            – RafaelC
            Nov 20 at 1:35















          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          You could do:



          import pandas as pd


          data = [[0, 'BananaMan', '09/10/2018'],
          [1, 'Superman247', '10/10/2009'],
          [2, '123456789', '08/03/2011'],
          [3, 'Nameless101', '07/12/2012']]

          df = pd.DataFrame(data=data, columns=['ID', 'IGName', 'Date_created'])

          df = df[~df['IGName'].str.isnumeric()]

          print(df)


          Output



             ID       IGName Date_created
          0 0 BananaMan 09/10/2018
          1 1 Superman247 10/10/2009
          3 3 Nameless101 07/12/2012


          From the documentation:




          Check whether all characters in each string in the Series/Index are
          numeric. Equivalent to str.isnumeric().




          Note that this solution assumes the column 'IGName' is of type string, otherwise you need to cast it to string, doing something like (as mentioned by @RafaelC):



          df['IGName'] = df['IGName'].astype(str)





          share|improve this answer














          You could do:



          import pandas as pd


          data = [[0, 'BananaMan', '09/10/2018'],
          [1, 'Superman247', '10/10/2009'],
          [2, '123456789', '08/03/2011'],
          [3, 'Nameless101', '07/12/2012']]

          df = pd.DataFrame(data=data, columns=['ID', 'IGName', 'Date_created'])

          df = df[~df['IGName'].str.isnumeric()]

          print(df)


          Output



             ID       IGName Date_created
          0 0 BananaMan 09/10/2018
          1 1 Superman247 10/10/2009
          3 3 Nameless101 07/12/2012


          From the documentation:




          Check whether all characters in each string in the Series/Index are
          numeric. Equivalent to str.isnumeric().




          Note that this solution assumes the column 'IGName' is of type string, otherwise you need to cast it to string, doing something like (as mentioned by @RafaelC):



          df['IGName'] = df['IGName'].astype(str)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 at 1:38

























          answered Nov 20 at 1:25









          Daniel Mesejo

          9,9631923




          9,9631923












          • This will fail if numbers are actually numbers (int or float type).
            – RafaelC
            Nov 20 at 1:29












          • @RafaelC do you mean if the column is of type int or float?
            – Daniel Mesejo
            Nov 20 at 1:31










          • I mean if you have mixed types in the same column. For example, if in your data, you had [2, 123456789, '08/03/2011'] instead of [2, '123456789', '08/03/2011']
            – RafaelC
            Nov 20 at 1:33










          • Does it suffices to cast the column to string first?
            – Daniel Mesejo
            Nov 20 at 1:34










          • As a matter of fact, yes ;}. Use .astype(str)
            – RafaelC
            Nov 20 at 1:35




















          • This will fail if numbers are actually numbers (int or float type).
            – RafaelC
            Nov 20 at 1:29












          • @RafaelC do you mean if the column is of type int or float?
            – Daniel Mesejo
            Nov 20 at 1:31










          • I mean if you have mixed types in the same column. For example, if in your data, you had [2, 123456789, '08/03/2011'] instead of [2, '123456789', '08/03/2011']
            – RafaelC
            Nov 20 at 1:33










          • Does it suffices to cast the column to string first?
            – Daniel Mesejo
            Nov 20 at 1:34










          • As a matter of fact, yes ;}. Use .astype(str)
            – RafaelC
            Nov 20 at 1:35


















          This will fail if numbers are actually numbers (int or float type).
          – RafaelC
          Nov 20 at 1:29






          This will fail if numbers are actually numbers (int or float type).
          – RafaelC
          Nov 20 at 1:29














          @RafaelC do you mean if the column is of type int or float?
          – Daniel Mesejo
          Nov 20 at 1:31




          @RafaelC do you mean if the column is of type int or float?
          – Daniel Mesejo
          Nov 20 at 1:31












          I mean if you have mixed types in the same column. For example, if in your data, you had [2, 123456789, '08/03/2011'] instead of [2, '123456789', '08/03/2011']
          – RafaelC
          Nov 20 at 1:33




          I mean if you have mixed types in the same column. For example, if in your data, you had [2, 123456789, '08/03/2011'] instead of [2, '123456789', '08/03/2011']
          – RafaelC
          Nov 20 at 1:33












          Does it suffices to cast the column to string first?
          – Daniel Mesejo
          Nov 20 at 1:34




          Does it suffices to cast the column to string first?
          – Daniel Mesejo
          Nov 20 at 1:34












          As a matter of fact, yes ;}. Use .astype(str)
          – RafaelC
          Nov 20 at 1:35






          As a matter of fact, yes ;}. Use .astype(str)
          – RafaelC
          Nov 20 at 1:35














          up vote
          0
          down vote













          Use df[...]:



          print(df[~df['IGName'].str.isnumeric()])


          Or:



          print(df[df['IGName'].str.contains(r'D+')])


          Both Output:



             ID       IGName Date_created
          0 0 BananaMan 09/10/2018
          1 1 Superman247 10/10/2009
          3 3 Nameless101 07/12/2012


          If IGName has integers do:



          print(df[pd.to_numeric(df.IGName, errors='coerce').notnull()])





          share|improve this answer



















          • 1




            Same observation from above applies here
            – RafaelC
            Nov 20 at 1:36










          • @RafaelC Now is it good.
            – U9-Forward
            Nov 20 at 1:45















          up vote
          0
          down vote













          Use df[...]:



          print(df[~df['IGName'].str.isnumeric()])


          Or:



          print(df[df['IGName'].str.contains(r'D+')])


          Both Output:



             ID       IGName Date_created
          0 0 BananaMan 09/10/2018
          1 1 Superman247 10/10/2009
          3 3 Nameless101 07/12/2012


          If IGName has integers do:



          print(df[pd.to_numeric(df.IGName, errors='coerce').notnull()])





          share|improve this answer



















          • 1




            Same observation from above applies here
            – RafaelC
            Nov 20 at 1:36










          • @RafaelC Now is it good.
            – U9-Forward
            Nov 20 at 1:45













          up vote
          0
          down vote










          up vote
          0
          down vote









          Use df[...]:



          print(df[~df['IGName'].str.isnumeric()])


          Or:



          print(df[df['IGName'].str.contains(r'D+')])


          Both Output:



             ID       IGName Date_created
          0 0 BananaMan 09/10/2018
          1 1 Superman247 10/10/2009
          3 3 Nameless101 07/12/2012


          If IGName has integers do:



          print(df[pd.to_numeric(df.IGName, errors='coerce').notnull()])





          share|improve this answer














          Use df[...]:



          print(df[~df['IGName'].str.isnumeric()])


          Or:



          print(df[df['IGName'].str.contains(r'D+')])


          Both Output:



             ID       IGName Date_created
          0 0 BananaMan 09/10/2018
          1 1 Superman247 10/10/2009
          3 3 Nameless101 07/12/2012


          If IGName has integers do:



          print(df[pd.to_numeric(df.IGName, errors='coerce').notnull()])






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 at 1:45

























          answered Nov 20 at 1:22









          U9-Forward

          10.5k2834




          10.5k2834








          • 1




            Same observation from above applies here
            – RafaelC
            Nov 20 at 1:36










          • @RafaelC Now is it good.
            – U9-Forward
            Nov 20 at 1:45














          • 1




            Same observation from above applies here
            – RafaelC
            Nov 20 at 1:36










          • @RafaelC Now is it good.
            – U9-Forward
            Nov 20 at 1:45








          1




          1




          Same observation from above applies here
          – RafaelC
          Nov 20 at 1:36




          Same observation from above applies here
          – RafaelC
          Nov 20 at 1:36












          @RafaelC Now is it good.
          – U9-Forward
          Nov 20 at 1:45




          @RafaelC Now is it good.
          – U9-Forward
          Nov 20 at 1:45


















          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%2f53384890%2fhow-to-remove-rows-in-a-pandas-dataframe-with-a-specific-column-containing-numbe%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

          Wiesbaden

          Marschland

          Dieringhausen