Context:
I have a dictionary and a Dataframe.
categories = { "Transport": ["taxi", "b u s", "bike"],
"Housing": ["r-ent","jysk", "ikea"]}
data = { "Date": ["2020-09-29", "2020-09-29", "2020-09-29"],
"Amount": [-28.0, -20.0 , -13.7],
"Title": ["ny taxi", "brooklyn*ikea", "Burger Joint Co"],
"Category": [None, None, None]}
df = pd.DataFrame(data, columns = ["Date", "Amount", "Title", "Category"])
Problem:
For each row of the Dataframe, I need to check if the value in the Dataframe["Title"] column contains one of the values in the dictionary list. If the list item is found in the value then the Dataframe["Category"] column should take the Key of the list where the match was found.
For example, "taxi" is a keyword in the dictionary list under the key "Transportation". Therefore the row that has "ny taxi" should have "Transportation" in the Category column.
Starting Dataframe:
Date Amount Title Category
0 2020-09-29 -28.0 ny taxi None
1 2020-09-29 -20.0 brooklyn*ikea None
2 2020-09-29 -13.7 Burger Joint Co None
Desired Output:
Date Amount Title Category
0 2020-09-29 -28.0 ny taxi Transport
1 2020-09-29 -20.0 brooklyn*ikea Housing
2 2020-09-29 -13.7 Burger Joint Co Missing