I am struggling to find a "pythonic" way to make this logic work:
I have a dataframe of payment transactions with a column of strings( "beneficiary"):
| index | beneficiary |
|---|---|
| 12 | REWE SAGT DANKE. ... |
| 13 | NaN |
| 14 | OBI BAU- U. HEIMWER//BERLIN/DE / OBI SAGT DANKE |
| 15 | NETFLIX INTERNATIONAL B.V. |
I need to create another column in my dataframe which would be the category that every line belongs to.
| index | beneficiary | category |
|---|---|---|
| 12 | REWE SAGT DANKE. ... | Groceries |
| 14 | OBI BAU- U. HEIMWER//BERLIN/DE / OBI SAGT DANKE | Groceries |
| 15 | NETFLIX INTERNATIONAL B.V. | Entertainment |
I am thinking to make a dictionary like this below and somehow reverse look-up the sub-string values from the categories dict with the column above:
categories = {"Groceries": ["EDEKA", "REWE", "OBI"],
"Entertainment": ["NETFLIX"]}
The logic is: if sub-string "REWE" is in df['beneficiary'] then df['category'] equals the key of the dict element where the sub-string is.
I am open to other mapping logic.