用另一列 Pandas DataFrame 替换一列中的值 [英] Replace values from one column with another column Pandas DataFrame
本文介绍了用另一列 Pandas DataFrame 替换一列中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个以 id 为字符串的 Pandas 数据帧 df:我正在尝试创建 new_claim 和 new_description 列
我发现的最接近的 SO 是
I have a pandas dataframe df with ids as strings: I am trying to create the new_claim and new_description columns
Closest SO I found was Efficiently replace part of value from one column with value from another column in pandas using regex? but this uses split part, and since the description changes I was unable to generalize.
I can run a one off
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)
But if I have
date_reg = re.compile(r'\b'+df['claim']+r'\b')
Then I get "TypeError: 'Series' objects are mutable, thus they cannot be hashed"
Another approach I took
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)
which givesa TypeError: replace() takes no keyword arguments
解决方案
Using just the method pandas.replace():
df.old_id = df.old_id.fillna(0).astype('int')
list_old = list(map(str, df.old_id.tolist()))
list_new = list(map(str, df.external_id.tolist()))
df['new_claim'] = df.claim.replace(to_replace=['Claim ID: ' + e for e in list_old], value=['Claim ID: ' + e for e in list_new], regex=True)
df['new_description'] = df.description.replace(to_replace=['\* ' + e + '\\n' for e in list_old], value=['* ' + e + '\\n' for e in list_new], regex=True)
Produces the following output:
这篇关于用另一列 Pandas DataFrame 替换一列中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文