I'm having some issues regarding regexes matching in my dataframe. I want to create an additional column in the dataframe that matches the right RegexLabel if the String of column y contains substrings of the RegexGroceries | RegexFurniture
Here is the code to reproduce:
# Dataframe
data = {'X': [1, 2, 3, 4], 'Y': ["supermarkt", "ikea", "albert heijn", "vloer"]}
# Regexes
RegexGroceries = ["boni","supermarkt","nutri","company lukic","albert heijn"]
RegexFurniture = ["ikea","meubel","tuin","tapijt","carpet","auping","bed"]
# Labels and categories
RegexCategories = [RegexGroceries, RegexFurniture]
RegexLabels = ["Groceries", "Furniture"]
# Apply labels for all regexes
counter = 0
for i in RegexCategories:
for j in i:
data['RegexCategory'] = np.where(data['y'].str.contains(j), RegexLabels[0 + counter], "None")
counter = counter + 1
Somehow my df[RegexCategory] only returns None. Can anyone help me?
My output needs to look like this:
| X | Y | RegexCategory |
|---|---|---|
| 1 | supermarkt | Groceries |
| 2 | ikea | Furniture |
| 3 | albert heijn | Groceries |
| 4 | vloer | None |
|