私信  •  关注

Amirhos Imani

Amirhos Imani 最近创建的主题
Amirhos Imani 最近回复了
7 年前
回复了 Amirhos Imani 创建的主题 » 如何在pandas python中为字符串创建汇总列[duplicate]

更具本土特色的熊猫方法是应用如下替换功能:

def multiple_replace(dict, text):
  # 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) 

一旦定义了函数,就可以将其应用到数据文件中。

di = {1: "A", 2: "B"}
df['col1'] = df.apply(lambda row: multiple_replace(di, row['col1']), axis=1)