Python, Pandas - Issue applying function to a column in a dataframe to replace only certain items
<p>I have a dictionary of abbreviations of some city names that our system (for some reason) applies to data (i.e. 'Kansas City' is abbreviated 'Kansas CY', and Oklahoma City is spelled correctly).</p>

<p>I am having an issue getting my function to apply to the column of the dataframe but it works when I pass in strings of data. Code sample below:</p>

<pre><code>def multiple_replace(text, dict):
  # Create a regular expression  from the dictionary keys
  regex = re.compile("(%s)" % "|".join(map(re.escape, dict.keys())))

  # For each match, look-up corresponding value in dictionary
  return regex.sub(lambda mo: dict[mo.string[mo.start():mo.end()]], text)

testDict = {"Kansas CY": "Kansas City"}

dfData['PREV_CITY'] = dfData['PREV_CITY'].apply(multiple_replace, dict=testDict)
</code></pre>

<p>When I add 'axis=1' into that last line it errors out saying I provided too many args. Otherwise, it runs without error it just doesn't make the changes when there is a match to the dictionary.</p>

<p>Thank you in advance!
-Reece</p>

<p>You can use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.map.html" rel="nofollow"><code>map</code></a> and pass a dict to replace exact matches against the dict keys with the dict values, as you may have case-sensitive matches I'd <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.lower.html" rel="nofollow"><code>lower</code></a> all the strings first prior to the match:</p>

<pre><code>dfData['PREV_CITY'] = dfData['PREV_CITY'].str.lower().map(testDict, na_action='ignore')
</code></pre>

<p>this assumes the keys in your dict are also lower case</p>

python pandas data-munging