Python:在pandas中的每一行中用curl括号替换角括号及其内的字符串

Python:在pandas中的每一行中用curl括号替换角括号及其内的字符串,python,pandas,Python,Pandas,这有一个参考 为了新颖起见,我对数据帧进行了一个小改动: ID Static_Text Params 1 Today, <adj1> is quite Sunny. Tomorrow, <adj2> 1-10-2020 may be little <adj3> 1 Today, <adj1>

这有一个参考

为了新颖起见,我对数据帧进行了一个小改动:

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
我正在尝试以下方法:

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)
def更换角度(df):
如果df['Static_Text']中的'':
rep\u txt=re.sub(r'\]*\>',{},df[Static\u Text']
返回rep_txt
df=df.应用(λx:替换角(x),轴=1)

但是我不太确定上面的代码片段。特别是如何在{}中引入0,1等。

IIUC您可以在
str.replace
中传递
lambda
函数作为替换:

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
df[“Static_Text”].str.replace(r“”,lambda m:“{'+f'{int(m.group(1))-1}'+'}”)
今天,{0}阳光明媚。明天,{1}可能很小{2}
1今天,{0}阳光充足。明天,{1}可能很小{2}
2今天,{0}阳光充足。明天,{1}可能很小{2}
3如果{1}没有答复,我们就在{0}附近喝杯咖啡休息一下
4如果我在{1}分钟内没有得到任何答复,我们就在{0}附近喝杯咖啡休息一下吧

如果adj中的数字超过9,则应将“m.group(0)[-2]”更改为“m.group(0)[4:-1]”

print(df[“Static_Text”].str.replace(r“”,lambda m:“{+f”{int(m.group(0)[4:-1])-1}+“}”))

非常感谢。两个问题:1。当您使用
str.replace(r“”
部分时,我可以使用我的正则表达式吗?因为单词
adj
可能会改变。2.
{int(m.group(0)[-2])-1}
到底在做什么?如果我有0…15而不是0,1,2呢?对于正则表达式,我可能会使用
r“”
。我更新了上面的内容并使用了
m.group(1)
相反,它应该适应任何数字。您的正则表达式会捕获
中的任何空格或特殊字符吗?比如
?我可以使用
r”“
这个正则表达式吗?非常感谢。我不希望
adj
成为正则表达式的一部分,因为这可能会改变。其次,如果我有15个参数,即{0},{1}…{15}
int(m.group(0)[4:-1]
有什么帮助?
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