I have a dictionary consisting of several keys that have multiple values assigned to them:
dict ={'key1':'val1(ex1)|val2','key2':'val3 (ex2)|val4','key3':'val5','key4':'val6|val7|val8|val9'}
The single keys consist of values having different lengths, that means that the count of elements in a value expression separated by "|" differs for the keys.Therefore, conversion to a dataframe is not possible as all arrays have to have the same length.
Then I have a dataframe in where one columns contains the values of the dictionary above.So target is to iterate over all rows in the dataframe column, match the value with the dictionary values and enter the corresponding key to a new column in the dataframe.
So far I use the following loop:
for key, val in dict.items(): df.loc[(df['Values'].str.contains(val)),'new_col'] = key
This seems to work partly already but sometimes (not always) values are not matched, especially if they contain parenthesis (like 'val1(example)' ).
End result should look like this:enter image description here
Would it be better to create a list of values in the dictionary?How should it work as my first attempt got me an error saying list are not hashable?
Thanks a lot!