How to substitute a particular word followed by a alphanumeric / numeric in pandas python?
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
add a comment |
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
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
add a comment |
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
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
python regex pandas text replace
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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
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
add a comment |
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))
add a comment |
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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))
add a comment |
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))
add a comment |
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))
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))
edited Nov 14 '18 at 14:55
answered Nov 14 '18 at 13:55
JoeJoe
6,01921429
6,01921429
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 14 '18 at 14:17
Maarten FabréMaarten Fabré
5,0201822
5,0201822
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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