Below is my input string
df = text codes
anesthesia code 99921
aretic closure 75454
nyopia 75633
right atrial 89732
string = However, when performed anesthesia code with a formal report, this service may be considered a significant, separately identifiable, and if nyopia, a separately reportable service.
In input string if we get any text from text column then we can replace with that perticular code from code column.
I want output like
output = However, when performed 99921 with a formal report, this service may be considered a significant, separately identifiable, and if 75633, a separately reportable service.
So how I can do this.
CodePudding user response:
Using a single regular expression that matches all of the texts should be quite efficient:
import re
import pandas as pd
df = pd.DataFrame(
{
"text": ["anesthesia code", "aretic closure", "nyopia", "right atrial"],
"codes": [99921, 75454, 75633, 89732],
}
)
# Generate a mapping of text to code
text_to_codes = {text: str(code) for text, code in zip(df["text"], df["codes"])}
# Generate a regexp that will match any `text`s
match_re = re.compile("|".join(re.escape(t) for t in text_to_codes))
string = """
However, when performed anesthesia code with a formal report,
this service may be considered a significant, separately identifiable,
and if nyopia, a separately reportable service.
"""
# Run the regexp on the string, replacing texts with what we find in the mapping
output = match_re.sub(lambda m: text_to_codes[m.group(0)], string)
print(output)
This outputs
However, when performed 99921 with a formal report,
this service may be considered a significant, separately identifiable,
and if 75633, a separately reportable service.
If you add flags=re.I
to the compile
call, this becomes case-insensitive too.
CodePudding user response:
Do you have to use pandas? If not, just put the data in a dictionary.
string = 'However, when performed anesthesia code with a formal report, this service may be considered a significant, separately identifiable, and if nyopia, a separately reportable service.'
dict = {
'anesthesia code': '99921',
'aretic closure': '75454',
'nyopia' : '75633',
'right atrial' : '89732'
}
for key in dict:
if key in string:
string = string.replace(key, dict[key])
print(string)
CodePudding user response:
Try
d = {
'anesthesia code': '99921',
'aretic closure': '75454',
'nyopia' : '75633',
'right atrial' : '89732'
}
print(f"However, when performed {d['anesthesia code']} with a formal report, this service may be considered a significant, separately identifiable, and if {d['nyopia']}, a separately reportable service.")