首页 > 解决方案 > Python:在熊猫的每一行中用大括号替换尖括号及其字符串

问题描述

这有对这个 SO 线程的引用。

为了新颖起见,我正在复制数据框,并进行了一些小改动:

ID         Static_Text                                           Params
1      Today, <adj1> is quite Sunny. Tomorrow, <adj2>           1-10-2020  
       may be little <adj3>
1      Today, <adj1> is quite Sunny. Tomorrow, <adj2>           2-10-2020
       may be little <adj3>
1      Today, <adj1> is quite Sunny. Tomorrow, <adj2>           Cloudy
       may be little <adj3>
2      Let's have a coffee break near <adj1>, if I              Balcony
       don't get any SO reply by <adj2>
2      Let's have a coffee break near <adj1>, if I               30
       don't get any SO reply by <adj2> mins

现在我想用<adj>{}一次出现的 ie<adj1>替换为{0}. 因此生成的数据框如下所示:

ID         Static_Text                                           Params
1      Today, {0} is quite Sunny. Tomorrow, {1}              1-10-2020  
       may be little {2}
1      Today, {0} is quite Sunny. Tomorrow, {1}              2-10-2020
       may be little {2}
1      Today, {0} is quite Sunny. Tomorrow, {1}              Cloudy
       may be little {2}
2      Let's have a coffee break near {0}, if I              Balcony
       don't get any SO reply by {1}
2      Let's have a coffee break near {0}, if I              30
       don't get any SO reply by {1} mins 

我正在尝试以下操作:

def replace_angular(df):
   if '<' and '>' in df['Static_Text']:
       rep_txt = re.sub(r'\<[^>]*\>',{},df[Static_Text'])
   return rep_txt

df = df.apply(lambda x : replace_angular(x),axis=1)

但我不太确定上面的代码片段。尤其是如何将 0,1 等带入 {}。

标签: pythonpandas

解决方案


IIUC 你可以传递一个lambda函数str.replace作为替换:

df["Static_Text"].str.replace(r"<[A-Za-z]+(\d+)>", lambda m: '{'+f'{int(m.group(1))-1}'+'}')

0                     Today, {0} is quite Sunny. Tomorrow, {1} may be little {2}
1                     Today, {0} is quite Sunny. Tomorrow, {1} may be little {2}
2                     Today, {0} is quite Sunny. Tomorrow, {1} may be little {2}
3         Let's have a coffee break near {0}, if I don't get any SO reply by {1}
4    Let's have a coffee break near {0}, if I don't get any SO reply by {1} mins

推荐阅读