【发布时间】:2019-07-16 17:20:12
【问题描述】:
我有一个带有 id 作为字符串的 pandas 数据框 df: 我正在尝试创建 new_claim 和 new_description 列
我找到的最接近的 SO 是 Efficiently replace part of value from one column with value from another column in pandas using regex?,但这使用了拆分部分,并且由于描述发生了变化,我无法一概而论。
我可以一劳永逸
date_reg = re.compile(r'\b'+df['old_id'][1]+r'\b')
df['new_claim'] = df['claim'].replace(to_replace=date_reg, value=df['external_id'], inplace=False)
如果我有
date_reg = re.compile(r'\b'+df['claim']+r'\b')
然后我得到“TypeError: 'Series' 对象是可变的,因此它们不能被散列”
我采取的另一种方法
df['new_claim'] = df['claim']
for i in range(5):
old_id = df['old_id'][i]
new_id = df['external_id'][i]
df['new_claim'][i] = df['claim'][i].replace(to_replace=old_id,value=new_id)
产生类型错误:replace() 不接受关键字参数
【问题讨论】:
标签: python regex pandas replace