简体   繁体   中英

Replace string with corresponding string from another column with pandas

I have a data frame called df that looks something like this:

pd.DataFrame({
    'column1' : ['client#1 is #name#', 'client#2 is #name#'], 
    'column2': ['josh', 'max']}
)

              column1 column2
0  client#1 is #name#    josh
1  client#2 is #name#     max

I am trying to replace the phrase " #name " in column1 with the value of column2. I want the end result to look like this:

在此处输入图片说明

I have tried a few approaches like the following:

df['column1'] = df['column1'].replace(["#name#"], df['column2'])

But I am not sure of how to grab the specific phrase ' #name# ' in column1 and replace it with the value of column2. Any suggestions on how to approach this would be greatly appreciated!

If it's strings, and if there are no NaNs, I would recommend calling str.replace inside a list comprehension for speed:

df['column1'] = [
    x.replace('#name#', y) for x, y in zip(df.column1, df.column2)]

df
            column1 column2
0  client#1 is josh    josh
1   client#2 is max     max

Why are list comprehensions worth it for string operations? You can read more at For loops with pandas - When should I care? .


Another interesting option you can consider is str.replace with iter :

it = iter(df.column2)
df['column1'] = df.column1.str.replace('#name#', lambda x: next(it))

df
            column1 column2
0  client#1 is josh    josh
1   client#2 is max     max

Should handle NaNs and mixed dtypes just fine (but will be slower).


A simpler replace option by @Vaishali, which will work if the "#name#" substring is always at the end of the string.

df['column1'] = df.column1.add(df.column2).str.replace('#name#', '')
df
            column1 column2
0  client#1 is josh    josh
1   client#2 is max     max

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