I need to append a new column to a pandas.DataFrame()
with the result on each row to be a switch case of different regEx.
New to python I discover that there is now switch/case loop so I have to do it with if/elif/else.
My first approach (and fail) was to do :
df = pd.DataFrame(data, columns=headers)
...
wrangle = wd()
df['groupe_canaux'] = wrangle.regex_canaux(df)
Then in my wrangle class :
class WrangleData:
...
def regex_canaux(self, df):
if df['medium'] == "(NONE)":
return "Direct"
elif df['medium'] == "(NOT SET)":
return "BAR"
elif re.match("(.*)app_id=cpcg$", df['landingPage']):
return "SEA"
else:
return "FOO"
I have ~20 regEx to add inn the final version checking up to 4 differents col values to add the good string in the new column.
Here I got the error : ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
EDIT : Further search lead me to DataFrame.apply()
. But I have no idea how to use it.