I have a dictionary of where the keys are numbers and the values are lists of strings. I want to create a dataframe column where the column values are the dictionary keys and the key is selected base on matching the value of another column in each row to an item in the dictionary value lists. See example code below: Sample starting dataframe and dictionary:
dict_x = {1:[a], 2:[b, c, e], 3:[d, f]
df = ['ID':[a, b, c, d, e, f]]
Desired output:
df = ['ID':[a, b, c, d, e, f], 'Number':[1, 2, 2, 3, 2, 3]]
I thought some sort of df['Number'] = df['ID'].apply(lambda x : ???) would work but I'm struggling with the conditions here, and I tried writing some for loops but ran in to issues with only the last iteration of the loop being preserved when I wrote the column.
