简体   繁体   中英

Replace values from one column with another column Pandas DataFrame

I have a pandas dataframe df with ids as strings: I am trying to create the new_claim and new_description columns

示例 df

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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM