Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to split multiple sentences and encase them in double quotes based on a period in python

I have a dataframe with columns. In my pattern column, there are multiple sentences that end with a period. I need each sentence to have a have a double quote around it.

Below is the orignal text

patterns
Keep related supplies in the same area., Make an effort to clean a dedicated workspace after every session., Place loose supplies in large, clearly visible containers., Use clotheslines and clips to hang sketches, photos, and reference material., Use every inch of the room for storage, especially vertical space., Use chalkboard paint to make space for drafting ideas right on the walls., Purchase a label maker to make your organization strategy semi permanent., Make a habit of throwing out old, excess, or useless stuff each month.

I need the results to be:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

patterns
"Keep related supplies in the same area.", "Make an effort to clean a dedicated workspace after every session.", "Place loose supplies in large, clearly visible containers.", "Use clotheslines and clips to hang sketches, photos, and reference material.", "Use every inch of the room for storage, especially vertical space.", "Use chalkboard paint to make space for drafting ideas right on the walls.", "Purchase a label maker to make your organization strategy semi permanent.", "Make a habit of throwing out old, excess, or useless stuff each month."

What is did already,

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

>Solution :

IIUC, you can do:

df["patterns"] = (
    df["patterns"]
    .str.findall(r"[A-Z].*?(?=,\s*[A-Z]|\Z)")
    .apply(lambda s: ", ".join(map(lambda s: f'"{s}"', s)))
)

Prints:

patterns
"Keep related supplies in the same area.", "Make an effort to clean a dedicated workspace after every session.", "Place loose supplies in large, clearly visible containers.", "Use clotheslines and clips to hang sketches, photos, and reference material.", "Use every inch of the room for storage, especially vertical space.", "Use chalkboard paint to make space for drafting ideas right on the walls.", "Purchase a label maker to make your organization strategy semi permanent.", "Make a habit of throwing out old, excess, or useless stuff each month."
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading