title: Use dictionary to replace a string within a string in Pandas columns
body: <p>I am trying to use a <code>dictionary</code> <code>key</code> to replace <code>strings</code> in a <code>pandas</code> column with its <code>values</code>. However, each column contains sentences. Therefore, I must first tokenize the sentences and detect whether a Word in the sentence corresponds with a key in my dictionary, then replace the string with the corresponding value.</p>
  
  <p>However, the result that I continue to get it none. Is there a better pythonic way to approach this problem?</p>
  
  <p>Here is my MVC for the moment. In the comments, I specified where the issue is happening. </p>
  
  <pre><code>import pandas as pd
  
  data = {'Categories': ['animal','plant','object'],
      'Type': ['tree','dog','rock'],
          'Comment': ['The NYC tree is very big','The cat from the UK is small','The rock was found in LA.']
  }
  
  ids = {'Id':['NYC','LA','UK'],
        'City':['New York City','Los Angeles','United Kingdom']}
  
  
  df = pd.DataFrame(data)
  ids = pd.DataFrame(ids)
  
  def col2dict(ids):
      data = ids[['Id', 'City']]
      idDict = data.set_index('Id').to_dict()['City']
      return idDict
  
  def replaceIds(data,idDict):
      ids = idDict.keys()
      types = idDict.values()
      data['commentTest'] = data['Comment']
      words = data['commentTest'].apply(lambda x: x.split())
      for (i,word) in enumerate(words):
          #Here we can see that the words appear
          print word
          print ids
          if word in ids:
          #Here we can see that they are not being recognized. What happened?
              print ids
              print word
              words[i] = idDict[word]
              data['commentTest'] = ' '.apply(lambda x: ''.join(x))
      return data
  
  idDict = col2dict(ids)
  results = replaceIds(df, idDict)
  </code></pre>
  
  <p>Results:</p>
  
  <pre><code>None
  </code></pre>
  
  <p>I am using <code>python2.7</code> and when I am printing out the <code>dict</code>, there are <code>u'</code> of Unicode. </p>
  
  <p>My expected outcome is:</p>
  
  <p>Categories</p>
  
  <p>Comment</p>
  
  <p>Type</p>
  
  <p>commentTest</p>
  
  <pre><code>  Categories  Comment  Type commentTest
  0 animal  The NYC tree is very big tree The New York City tree is very big 
  1 plant The cat from the UK is small dog  The cat from the United Kingdom is small 
  2 object  The rock was found in LA. rock  The rock was found in Los Angeles. 
  </code></pre>