title: Pandas dataframe look for key in dict, write key in new column according to value
body: <p>I have a dataframe:</p>
  <pre><code>df = pd.DataFrame([
                     {'ID': 1,'A': [{'name': 'lifestyle'}, {'name': 'economy'}, 
                            {'name': 'politics'}, {'name': 'climate &amp; environment'}]}, 
                     {'ID': 2,'A': [{'name': 'sport'}]}, 
                     {'ID': 3,'A': [{'name': 'climate &amp; environment'}]},
                     {'ID': 4,'A': [{'name': 'sport'}]},
                     {'ID': 5,'A': [{'name': 'politics'}, {'name': 'world'}]},
                     {'ID': 6,'A': [{'name': 'economy'}, {'name': 'politics'}]}
                    ])
  </code></pre>
  <p>Every value in col A belongs to a category. Those categories are in a hardcoded dict (categories.txt):</p>
  <pre><code>dict= {'lifestyle':'cat1',
  'economy':'cat1',
  'politics':'cat2',
  'climate &amp; environment':'cat2',
  'sport':'cat3',
  'world':'cat4',
  'news':'cat3'}
  </code></pre>
  <p>My goal is to look for every key and write this key(s) in a new col named after the value (cat1, cat2,...)</p>
  <p>Here is what I got so far:</p>
  <pre><code>df['A'] = [','.join(map(str, l)) for l in df['A']]
  </code></pre>
  <pre><code># read in the dict
  d = {}
  with open(&quot;categories.txt&quot;, &quot;r&quot;) as file:
      for line in file:
          key, value = line.strip().split(&quot;:&quot;)
          d[key] = value
  
  
  di = {k: oldk for oldk, oldv in d.items() for k in oldv.split(',')}
  
  
  for k, v in d.items():
      if v == 'cat1':
          df.loc[df['A'].str.contains(k), 'cat1'] = k
      elif v == 'cat2':
          df.loc[df['A'].str.contains(k), 'cat2'] = k
      elif v == 'cat3':
          df.loc[df['A'].str.contains(k), 'cat3'] = k 
      else:
          df.loc[df['A'].str.contains(k), 'cat4'] = k
  </code></pre>
  <p>Right now, if there is more than one key per row this will be written as the next category which is not the right way. How do I get every key (one or multiple keys per cell) in the right column named after the values?
  Something like this:</p>
  <pre><code>df = pd.DataFrame({'ID':[1,2,3,4,5,6],
                     'cat1':['lifestyle, economy','nan','nan','nan','nan','economy, politics'],
                     'cat2':['politics, climate &amp; environment','nan','climate &amp; environment','nan','politics','nan'],
                     'cat3':['nan','sport','nan','sport','nan','nan'],
                     'cat4':['nan','nan','nan','nan','world','nan']})
  </code></pre>
  <p>Thanks in advance</p>