简体   繁体   English

Python - Pandas - 根据其他列的值替换列中的字符串 - 处理子字符串

[英]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基于以下帖子: Python - Pandas - 根据其他列的值替换列中的字符串

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.我想要的是用 col2 中的值替换 col2 上存在的 col0 值。

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?我可以在 .values 上添加更多精度来实现这一点吗?

Thanks!谢谢!

Use re.sub with replace by rows in DataFrame.apply :使用re.subDataFrame.apply的行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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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