Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Find keys of a nested dictionary by searching a string in the values of the dictionary

I have a dataframe as follows

------------------------------
Name        |       countries 
-----------------------------
Batman      |       India
Superman    |       Canada
Spiderman   |       Australia 

Search String - Dictionary :-

keywords = {
Male : 
    [
        { Maxwell : [ India , US ]  } ,
        { George  : [ France , Germany , Spain ] } 
    ],

Female : 
    [ 
      { Martha :  [ Australia ] }
    ]  
}

Problem Statement:-

For each row of the dataframe , I need to search the countries column with the words in the "Dict value " - and wherever there is a match, need to return the first level key and its parent level key

Expected output

-----------------
------------------------------------------------------------------------
Name        |       countries       |       level2      |       level1
------------------------------------------------------------------------
Batman      |       India           |       Maxwell     |       Male 
Superman    |       Canada          |       NaN         |       NaN
Spiderman   |       Australia       |       Martha      |       Female 

Comments