简体   繁体   中英

Python - Pandas - Replace a string from a column based on the value from other column - Dealing with substrings

Based on the following post: Python - Pandas - Replace a string from a column based on the value from other column

I was doing some similar on my side and I have faced new challenges. I use the same example as the previous post.

The new challenge that consists with the substrings.

Imagine that I have the following dataframe:

在此处输入图片说明

What I am trying is to replace on col2 the values that exists on col0 with the values from col2.

If I use the code (it is the same from the previous post):

df['col3'] = df['col1'].replace(df['col0'].values, df['col2'].values, regex = True)

I will return the following dataframe:

在此处输入图片说明

And what I am trying is the following one:

在此处输入图片说明

Can I add some more precision on .values to achieve this?

Thanks!

Use re.sub with replace by rows in DataFrame.apply :

import re

df['col3'] = df.apply(lambda x: re.sub(x['col0'],x['col2'],x['col1']), axis=1)

Or in list comprehension:

df['col3'] = [re.sub(a,c,b) for a,b,c in df[['col0','col1','col2']].to_numpy()]

print (df)
        col0                  col1     col2                col3
0    Table 1    Tablename: Table 1  Table A  Tablename: Table A
1    Table 2    Tablename: Table 2  Table B  Tablename: Table B
2  Table 2_1  Tablename: Table 2_1  Table C  Tablename: Table C

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