How can I get the specific strings in column value

Question:

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.

Asked By: Shamna Sama

||

Answers:

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')
Answered By: Tim Biegeleisen
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.