Python,Pandas-向数据帧中的列应用函数以仅替换某些项

Python,Pandas-向数据帧中的列应用函数以仅替换某些项,python,pandas,data-munging,Python,Pandas,Data Munging,我有一本我们的系统(出于某种原因)适用于数据的一些城市名称的缩写词典(即“堪萨斯城”缩写为“堪萨斯西”,俄克拉荷马城拼写正确) 我在将函数应用于dataframe的列时遇到了一个问题,但当我传入数据字符串时,它会工作。代码示例如下: def multiple_replace(text, dict): # Create a regular expression from the dictionary keys regex = re.compile("(%s)" % "|".join(ma

我有一本我们的系统(出于某种原因)适用于数据的一些城市名称的缩写词典(即“堪萨斯城”缩写为“堪萨斯西”,俄克拉荷马城拼写正确)

我在将函数应用于dataframe的列时遇到了一个问题,但当我传入数据字符串时,它会工作。代码示例如下:

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)
当我在最后一行中添加'axis=1'时,它错误地指出我提供了太多参数。否则,它运行时不会出错,只是在与字典匹配时不会进行更改

提前谢谢你! -Reece

您可以使用并传递dict来用dict值替换dict键的精确匹配,因为您可能有区分大小写的匹配,我会在匹配之前首先将所有字符串:

dfData['PREV_CITY'] = dfData['PREV_CITY'].str.lower().map(testDict, na_action='ignore')

这假设dict中的键也是小写的

如果您已经有dict,那么
dfData['PREV_CITY']=dfData['PREV_CITY'].map(testDict,na_action='ignore')
应该可以:-(它刚刚清除了列,这意味着您没有与dict密钥完全匹配的数据,您还没有发布任何样本数据,因此这只是一个猜测,真该死……它区分大小写……我觉得自己像个傻瓜。谢谢!!