0

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.

Ragnar
  • 2,550
  • 6
  • 36
  • 70

1 Answers1

2

I finally found out how to do it with df.apply() thanks to this post : How to apply a function to two columns of Pandas dataframe

data_to_df.py

df = pd.DataFrame(data, columns=headers)
...
wrangle = wd()
df['groupe_canaux'] = df.apply(lambda x: wrangle.regex_canaux(x.medium), axis=1)

wrangle_data.py

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"

Then after a print(df.head()) I got :

        date          medium  sessions groupe_canaux
0 2019-02-25          (NONE)     11173        Direct
1 2019-02-25       (NOT SET)        12           BAR
2 2019-02-25  ABTESTING-HOME       126           FOO
3 2019-02-25       AFFILIATE         1           FOO
4 2019-02-25         ANNONCE         1           FOO
Ragnar
  • 2,550
  • 6
  • 36
  • 70