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.
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')
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.
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')