如果另一列中的项匹配,如何迭代数据帧并替换字符串

2024-04-25 07:15:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我的df数据有两列,如下所示

thePerson  theText
"the abc" "this is about the abc"
"xyz" "this is about tyu"
"wxy" "this is about abc"
"wxy" "this is about WXY"

我想要一个结果df

thePerson  theText
"the abc" "this is about <b>the abc</b>"
"xyz" "this is about tyu"
"wxy" "this is about abc"
"wxy" "this is about <b>WXY</b>"

请注意,如果同一行中的文本包含人物,则文本中的人物将变为粗体。你知道吗

我尝试过的一个失败的解决方案是:

df['theText']=df['theText'].replace(df.thePerson,'<b>'+df.thePerson+'</b>', regex=True)

我想知道我是否可以用lapplymap来做这个

我的python环境设置为2.7版


Tags: the数据文本dfisthisaboutabc
2条回答

您可以使用^{}

df['theText'] = df.apply(lambda x: re.sub(r'('+x.thePerson+')',
                                          r'<b>\1</b>', 
                                          x.theText, 
                                          flags=re.IGNORECASE), axis=1)
print (df)
  thePerson                       theText
0   the abc  this is about <b>the abc</b>
1       xyz             this is about tyu
2       wxy             this is about abc
3       wxy      this is about <b>WXY</b>

使用re.subzip

tt = df.theText.values.tolist()
tp = df.thePerson.str.strip('"').values.tolist()
df.assign(
    theText=[re.sub(r'({})'.format(p), r'<b>\1</b>', t, flags=re.I)
             for t, p in zip(tt, tp)]
)

  thePerson                       theText
0   the abc  this is about <b>the abc</b>
1       xyz             this is about tyu
2       wxy             this is about abc
3       wxy      this is about <b>WXY</b>

复制/粘贴 您应该能够运行这个精确的代码并获得所需的结果

from io import StringIO
import pandas as pd

txt = '''thePerson  theText
"the abc"  "this is about the abc"
"xyz"  "this is about tyu"
"wxy"  "this is about abc"
"wxy"  "this is about WXY"'''

df = pd.read_csv(StringIO(txt), sep='\s{2,}', engine='python')

tt = df.theText.values.tolist()
tp = df.thePerson.str.strip('"').values.tolist()
df.assign(
    theText=[re.sub(r'({})'.format(p), r'<b>\1</b>', t, flags=re.I)
             for t, p in zip(tt, tp)]
)

你应该看看这个

   thePerson                         theText
0  "the abc"  "this is about <b>the abc</b>"
1      "xyz"             "this is about tyu"
2      "wxy"             "this is about abc"
3      "wxy"      "this is about <b>WXY</b>"

相关问题 更多 >