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 can I get the specific strings in column value

what I want to do is delete certain parts of a string and take the only near of AcoS and insert it into a new column.

import pandas as pd


data = [{"Campaign" : "Sf l Spy l Branded l ACoS 20 l Manual NX"}]
df = pd.DataFrame(data)
df.insert(1,"targetAcos", 0)
df["targetAcos"] = df["Campaign"].str.replace(r' l ACoS \(.*)\l', r'\1', regex=True)

print(df["targetAcos"])

But I guess I am kinda bad at this, I couldn’t make it correctly so I hope you guys can explain how can you do.

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

>Solution :

I think the Pandas function you want to be using here is str.extract:

df["targetAcos"] = df["Campaign"].str.extract(r'\bl ACoS (\d+) l')

Or perhaps a more generic regex would be:

df["targetAcos"] = df["Campaign"].str.extract(r'\bACoS (\d+)\b')
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