How to substitute a particular word followed by a alphanumeric / numeric in pandas python?










0















How do I substitute a particular word(i.e. ABC) followed by a alphanumeric / numeric as shown below in pandas



Input data



what is ABC s123 doing 77 here?
what is abc aA574 doing 89 here?
what is ABC-X187 doing here?
what is aBC^984 doing here?
what is Abc647 doing here?


Expected output data



what is ABCS123 doing 77 here?
what is ABCAA574 doing 89 here?
what is ABCX187 doing here?
what is ABC984 doing here?
what is ABC647 doing here?


Note: Any alphanumeric can follow ABC. The numbers shown here are just example and dont hardcode the number in the solution.



EDIT1: Just tried the proposed solution. It doesn't work when the special character is space. So please remove the duplicate tag.



EDIT2: Kindly handle the case of ABC as per the question.










share|improve this question
























  • Do you want to remove the spaces or the punctuations?

    – Mohit Motwani
    Nov 14 '18 at 14:01












  • Both. Please see the example in the question. I just want to replicate that behaviour.

    – GeorgeOfTheRF
    Nov 14 '18 at 14:03
















0















How do I substitute a particular word(i.e. ABC) followed by a alphanumeric / numeric as shown below in pandas



Input data



what is ABC s123 doing 77 here?
what is abc aA574 doing 89 here?
what is ABC-X187 doing here?
what is aBC^984 doing here?
what is Abc647 doing here?


Expected output data



what is ABCS123 doing 77 here?
what is ABCAA574 doing 89 here?
what is ABCX187 doing here?
what is ABC984 doing here?
what is ABC647 doing here?


Note: Any alphanumeric can follow ABC. The numbers shown here are just example and dont hardcode the number in the solution.



EDIT1: Just tried the proposed solution. It doesn't work when the special character is space. So please remove the duplicate tag.



EDIT2: Kindly handle the case of ABC as per the question.










share|improve this question
























  • Do you want to remove the spaces or the punctuations?

    – Mohit Motwani
    Nov 14 '18 at 14:01












  • Both. Please see the example in the question. I just want to replicate that behaviour.

    – GeorgeOfTheRF
    Nov 14 '18 at 14:03














0












0








0


2






How do I substitute a particular word(i.e. ABC) followed by a alphanumeric / numeric as shown below in pandas



Input data



what is ABC s123 doing 77 here?
what is abc aA574 doing 89 here?
what is ABC-X187 doing here?
what is aBC^984 doing here?
what is Abc647 doing here?


Expected output data



what is ABCS123 doing 77 here?
what is ABCAA574 doing 89 here?
what is ABCX187 doing here?
what is ABC984 doing here?
what is ABC647 doing here?


Note: Any alphanumeric can follow ABC. The numbers shown here are just example and dont hardcode the number in the solution.



EDIT1: Just tried the proposed solution. It doesn't work when the special character is space. So please remove the duplicate tag.



EDIT2: Kindly handle the case of ABC as per the question.










share|improve this question
















How do I substitute a particular word(i.e. ABC) followed by a alphanumeric / numeric as shown below in pandas



Input data



what is ABC s123 doing 77 here?
what is abc aA574 doing 89 here?
what is ABC-X187 doing here?
what is aBC^984 doing here?
what is Abc647 doing here?


Expected output data



what is ABCS123 doing 77 here?
what is ABCAA574 doing 89 here?
what is ABCX187 doing here?
what is ABC984 doing here?
what is ABC647 doing here?


Note: Any alphanumeric can follow ABC. The numbers shown here are just example and dont hardcode the number in the solution.



EDIT1: Just tried the proposed solution. It doesn't work when the special character is space. So please remove the duplicate tag.



EDIT2: Kindly handle the case of ABC as per the question.







python regex pandas text replace






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 15:35







GeorgeOfTheRF

















asked Nov 14 '18 at 13:51









GeorgeOfTheRFGeorgeOfTheRF

1,48562751




1,48562751












  • Do you want to remove the spaces or the punctuations?

    – Mohit Motwani
    Nov 14 '18 at 14:01












  • Both. Please see the example in the question. I just want to replicate that behaviour.

    – GeorgeOfTheRF
    Nov 14 '18 at 14:03


















  • Do you want to remove the spaces or the punctuations?

    – Mohit Motwani
    Nov 14 '18 at 14:01












  • Both. Please see the example in the question. I just want to replicate that behaviour.

    – GeorgeOfTheRF
    Nov 14 '18 at 14:03

















Do you want to remove the spaces or the punctuations?

– Mohit Motwani
Nov 14 '18 at 14:01






Do you want to remove the spaces or the punctuations?

– Mohit Motwani
Nov 14 '18 at 14:01














Both. Please see the example in the question. I just want to replicate that behaviour.

– GeorgeOfTheRF
Nov 14 '18 at 14:03






Both. Please see the example in the question. I just want to replicate that behaviour.

– GeorgeOfTheRF
Nov 14 '18 at 14:03













3 Answers
3






active

oldest

votes


















3














You can use this code:



import re

regex = r"(.*[A-Z]+).*?(d+.*)"

test_str = """what is ABC 123 doing here?
what is ABC 574 doing here?
what is ABC-187 doing here?
what is ABC^984 doing here?
what is ABC647 doing here?"""

subst = r"12"

result = re.sub(regex, subst, test_str)
print (result)
# what is ABC123 doing here?
# what is ABC574 doing here?
# what is ABC187 doing here?
# what is ABC984 doing here?
# what is ABC647 doing here?


Details at regex101: https://regex101.com/r/gGK8fJ/2






share|improve this answer























  • Instead of "what" at the beginning of the sentence it could be any word. Please keep that in mind. Will this solution work then? I don't see you hard coding ABC so just checking

    – GeorgeOfTheRF
    Nov 14 '18 at 14:17












  • Let check, and tell me. If has any problems, I will update my solution for you!

    – protoproto
    Nov 14 '18 at 14:20











  • This works! However I only want the substitution to happen for word starting with abc only. Could you please update your solution to reflect the same?

    – GeorgeOfTheRF
    Nov 14 '18 at 15:15











  • You can set it in character set ([ ... ]). Example with this: regex = r"(.*[A-C]+).*?(d+.*)", just works with word starting with A --> C (mean A, B, or C)

    – protoproto
    Nov 14 '18 at 22:17


















3














You can use:



df['col'] = df['col'].str.replace(r'(?<=ABC)W+(?=ddd)', '')


or



df['col'] = df['col'].map(lambda x: re.sub(r'(?<=ABC)W+(?=ddd)', '', x))





share|improve this answer
































    2














    from the documentation of Series.str.replace



    s = pd.Series("""what is ABC 123 doing here?
    what is ABC 574 doing here?
    what is ABC-187 doing here?
    what is ABC^984 doing here?
    what is ABC647 doing here?""".split("n"))

    pattern = r"ABC.*?(d+)"
    s.str.replace(pattern, r"ABC 1")



    0 what is ABC 123 doing here?
    1 what is ABC 574 doing here?
    2 what is ABC 187 doing here?
    3 what is ABC 984 doing here?
    4 what is ABC 647 doing here?
    dtype: object






    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%2f53301825%2fhow-to-substitute-a-particular-word-followed-by-a-alphanumeric-numeric-in-pand%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      You can use this code:



      import re

      regex = r"(.*[A-Z]+).*?(d+.*)"

      test_str = """what is ABC 123 doing here?
      what is ABC 574 doing here?
      what is ABC-187 doing here?
      what is ABC^984 doing here?
      what is ABC647 doing here?"""

      subst = r"12"

      result = re.sub(regex, subst, test_str)
      print (result)
      # what is ABC123 doing here?
      # what is ABC574 doing here?
      # what is ABC187 doing here?
      # what is ABC984 doing here?
      # what is ABC647 doing here?


      Details at regex101: https://regex101.com/r/gGK8fJ/2






      share|improve this answer























      • Instead of "what" at the beginning of the sentence it could be any word. Please keep that in mind. Will this solution work then? I don't see you hard coding ABC so just checking

        – GeorgeOfTheRF
        Nov 14 '18 at 14:17












      • Let check, and tell me. If has any problems, I will update my solution for you!

        – protoproto
        Nov 14 '18 at 14:20











      • This works! However I only want the substitution to happen for word starting with abc only. Could you please update your solution to reflect the same?

        – GeorgeOfTheRF
        Nov 14 '18 at 15:15











      • You can set it in character set ([ ... ]). Example with this: regex = r"(.*[A-C]+).*?(d+.*)", just works with word starting with A --> C (mean A, B, or C)

        – protoproto
        Nov 14 '18 at 22:17















      3














      You can use this code:



      import re

      regex = r"(.*[A-Z]+).*?(d+.*)"

      test_str = """what is ABC 123 doing here?
      what is ABC 574 doing here?
      what is ABC-187 doing here?
      what is ABC^984 doing here?
      what is ABC647 doing here?"""

      subst = r"12"

      result = re.sub(regex, subst, test_str)
      print (result)
      # what is ABC123 doing here?
      # what is ABC574 doing here?
      # what is ABC187 doing here?
      # what is ABC984 doing here?
      # what is ABC647 doing here?


      Details at regex101: https://regex101.com/r/gGK8fJ/2






      share|improve this answer























      • Instead of "what" at the beginning of the sentence it could be any word. Please keep that in mind. Will this solution work then? I don't see you hard coding ABC so just checking

        – GeorgeOfTheRF
        Nov 14 '18 at 14:17












      • Let check, and tell me. If has any problems, I will update my solution for you!

        – protoproto
        Nov 14 '18 at 14:20











      • This works! However I only want the substitution to happen for word starting with abc only. Could you please update your solution to reflect the same?

        – GeorgeOfTheRF
        Nov 14 '18 at 15:15











      • You can set it in character set ([ ... ]). Example with this: regex = r"(.*[A-C]+).*?(d+.*)", just works with word starting with A --> C (mean A, B, or C)

        – protoproto
        Nov 14 '18 at 22:17













      3












      3








      3







      You can use this code:



      import re

      regex = r"(.*[A-Z]+).*?(d+.*)"

      test_str = """what is ABC 123 doing here?
      what is ABC 574 doing here?
      what is ABC-187 doing here?
      what is ABC^984 doing here?
      what is ABC647 doing here?"""

      subst = r"12"

      result = re.sub(regex, subst, test_str)
      print (result)
      # what is ABC123 doing here?
      # what is ABC574 doing here?
      # what is ABC187 doing here?
      # what is ABC984 doing here?
      # what is ABC647 doing here?


      Details at regex101: https://regex101.com/r/gGK8fJ/2






      share|improve this answer













      You can use this code:



      import re

      regex = r"(.*[A-Z]+).*?(d+.*)"

      test_str = """what is ABC 123 doing here?
      what is ABC 574 doing here?
      what is ABC-187 doing here?
      what is ABC^984 doing here?
      what is ABC647 doing here?"""

      subst = r"12"

      result = re.sub(regex, subst, test_str)
      print (result)
      # what is ABC123 doing here?
      # what is ABC574 doing here?
      # what is ABC187 doing here?
      # what is ABC984 doing here?
      # what is ABC647 doing here?


      Details at regex101: https://regex101.com/r/gGK8fJ/2







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 14 '18 at 14:11









      protoprotoprotoproto

      1,01879




      1,01879












      • Instead of "what" at the beginning of the sentence it could be any word. Please keep that in mind. Will this solution work then? I don't see you hard coding ABC so just checking

        – GeorgeOfTheRF
        Nov 14 '18 at 14:17












      • Let check, and tell me. If has any problems, I will update my solution for you!

        – protoproto
        Nov 14 '18 at 14:20











      • This works! However I only want the substitution to happen for word starting with abc only. Could you please update your solution to reflect the same?

        – GeorgeOfTheRF
        Nov 14 '18 at 15:15











      • You can set it in character set ([ ... ]). Example with this: regex = r"(.*[A-C]+).*?(d+.*)", just works with word starting with A --> C (mean A, B, or C)

        – protoproto
        Nov 14 '18 at 22:17

















      • Instead of "what" at the beginning of the sentence it could be any word. Please keep that in mind. Will this solution work then? I don't see you hard coding ABC so just checking

        – GeorgeOfTheRF
        Nov 14 '18 at 14:17












      • Let check, and tell me. If has any problems, I will update my solution for you!

        – protoproto
        Nov 14 '18 at 14:20











      • This works! However I only want the substitution to happen for word starting with abc only. Could you please update your solution to reflect the same?

        – GeorgeOfTheRF
        Nov 14 '18 at 15:15











      • You can set it in character set ([ ... ]). Example with this: regex = r"(.*[A-C]+).*?(d+.*)", just works with word starting with A --> C (mean A, B, or C)

        – protoproto
        Nov 14 '18 at 22:17
















      Instead of "what" at the beginning of the sentence it could be any word. Please keep that in mind. Will this solution work then? I don't see you hard coding ABC so just checking

      – GeorgeOfTheRF
      Nov 14 '18 at 14:17






      Instead of "what" at the beginning of the sentence it could be any word. Please keep that in mind. Will this solution work then? I don't see you hard coding ABC so just checking

      – GeorgeOfTheRF
      Nov 14 '18 at 14:17














      Let check, and tell me. If has any problems, I will update my solution for you!

      – protoproto
      Nov 14 '18 at 14:20





      Let check, and tell me. If has any problems, I will update my solution for you!

      – protoproto
      Nov 14 '18 at 14:20













      This works! However I only want the substitution to happen for word starting with abc only. Could you please update your solution to reflect the same?

      – GeorgeOfTheRF
      Nov 14 '18 at 15:15





      This works! However I only want the substitution to happen for word starting with abc only. Could you please update your solution to reflect the same?

      – GeorgeOfTheRF
      Nov 14 '18 at 15:15













      You can set it in character set ([ ... ]). Example with this: regex = r"(.*[A-C]+).*?(d+.*)", just works with word starting with A --> C (mean A, B, or C)

      – protoproto
      Nov 14 '18 at 22:17





      You can set it in character set ([ ... ]). Example with this: regex = r"(.*[A-C]+).*?(d+.*)", just works with word starting with A --> C (mean A, B, or C)

      – protoproto
      Nov 14 '18 at 22:17













      3














      You can use:



      df['col'] = df['col'].str.replace(r'(?<=ABC)W+(?=ddd)', '')


      or



      df['col'] = df['col'].map(lambda x: re.sub(r'(?<=ABC)W+(?=ddd)', '', x))





      share|improve this answer





























        3














        You can use:



        df['col'] = df['col'].str.replace(r'(?<=ABC)W+(?=ddd)', '')


        or



        df['col'] = df['col'].map(lambda x: re.sub(r'(?<=ABC)W+(?=ddd)', '', x))





        share|improve this answer



























          3












          3








          3







          You can use:



          df['col'] = df['col'].str.replace(r'(?<=ABC)W+(?=ddd)', '')


          or



          df['col'] = df['col'].map(lambda x: re.sub(r'(?<=ABC)W+(?=ddd)', '', x))





          share|improve this answer















          You can use:



          df['col'] = df['col'].str.replace(r'(?<=ABC)W+(?=ddd)', '')


          or



          df['col'] = df['col'].map(lambda x: re.sub(r'(?<=ABC)W+(?=ddd)', '', x))






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 14:55

























          answered Nov 14 '18 at 13:55









          JoeJoe

          6,01921429




          6,01921429





















              2














              from the documentation of Series.str.replace



              s = pd.Series("""what is ABC 123 doing here?
              what is ABC 574 doing here?
              what is ABC-187 doing here?
              what is ABC^984 doing here?
              what is ABC647 doing here?""".split("n"))

              pattern = r"ABC.*?(d+)"
              s.str.replace(pattern, r"ABC 1")



              0 what is ABC 123 doing here?
              1 what is ABC 574 doing here?
              2 what is ABC 187 doing here?
              3 what is ABC 984 doing here?
              4 what is ABC 647 doing here?
              dtype: object






              share|improve this answer



























                2














                from the documentation of Series.str.replace



                s = pd.Series("""what is ABC 123 doing here?
                what is ABC 574 doing here?
                what is ABC-187 doing here?
                what is ABC^984 doing here?
                what is ABC647 doing here?""".split("n"))

                pattern = r"ABC.*?(d+)"
                s.str.replace(pattern, r"ABC 1")



                0 what is ABC 123 doing here?
                1 what is ABC 574 doing here?
                2 what is ABC 187 doing here?
                3 what is ABC 984 doing here?
                4 what is ABC 647 doing here?
                dtype: object






                share|improve this answer

























                  2












                  2








                  2







                  from the documentation of Series.str.replace



                  s = pd.Series("""what is ABC 123 doing here?
                  what is ABC 574 doing here?
                  what is ABC-187 doing here?
                  what is ABC^984 doing here?
                  what is ABC647 doing here?""".split("n"))

                  pattern = r"ABC.*?(d+)"
                  s.str.replace(pattern, r"ABC 1")



                  0 what is ABC 123 doing here?
                  1 what is ABC 574 doing here?
                  2 what is ABC 187 doing here?
                  3 what is ABC 984 doing here?
                  4 what is ABC 647 doing here?
                  dtype: object






                  share|improve this answer













                  from the documentation of Series.str.replace



                  s = pd.Series("""what is ABC 123 doing here?
                  what is ABC 574 doing here?
                  what is ABC-187 doing here?
                  what is ABC^984 doing here?
                  what is ABC647 doing here?""".split("n"))

                  pattern = r"ABC.*?(d+)"
                  s.str.replace(pattern, r"ABC 1")



                  0 what is ABC 123 doing here?
                  1 what is ABC 574 doing here?
                  2 what is ABC 187 doing here?
                  3 what is ABC 984 doing here?
                  4 what is ABC 647 doing here?
                  dtype: object







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 14 '18 at 14:17









                  Maarten FabréMaarten Fabré

                  5,0201822




                  5,0201822



























                      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%2f53301825%2fhow-to-substitute-a-particular-word-followed-by-a-alphanumeric-numeric-in-pand%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







                      這個網誌中的熱門文章

                      What does pagestruct do in Eviews?

                      Dutch intervention in Lombok and Karangasem

                      Channel Islands