I have a list of column names that are in string format like below:
JavaScript
x
2
1
lst = ["plug", "[plug+wallet]", "(wallet-phone)"]
2
Now I want to add df[]
with " ' "
to each column name using regex and I did it which does that when the list has (wallet-phone)
this kind of string it gives an output like this df[('wallet']-df['phone')]
. How do I get like this (df['wallet']-df['phone']),
Is my pattern wrong. Please refer it below:
JavaScript
1
12
12
1
import re
2
lst = ["plug", "[plug+wallet]", "(wallet-phone)"]
3
x=[]
4
y=[]
5
for l in lst:
6
x.append(re.sub(r"([^+-*/'d]+)", r"'1'", l))
7
for f in x:
8
y.append(re.sub(r"('[^+-*/'d]+')", r'df[1]',f))
9
10
print(x)
11
print(y)
12
gives:
JavaScript
1
3
1
x:["'plug'", "'[plug'+'wallet]'", "'(wallet'-'phone)'"]
2
y:["df['plug']", "df['[plug']+df['wallet]']", "df['(wallet']-df['phone)']"]
3
Is the pattern wrong? Expected output:
JavaScript
1
3
1
x:["'plug'", "['plug'+'wallet']", "('wallet'-'phone')"]
2
y:["df['plug']", "[df['plug']+df['wallet']]", "(df['wallet']-df['phone'])"]
3
I also tried ([^+-*/()[]'d]+)
this pattern but it isn’t avoiding () or []
Advertisement
Answer
It might be easier to locate words and enclose them in the dictionary reference:
JavaScript
1
8
1
import re
2
lst = ["plug", "[plug+wallet]", "(wallet-phone)"]
3
4
z = [re.sub(r"(w+)",r"df['1']",w) for w in lst]
5
6
print(z)
7
["df['plug']", "[df['plug']+df['wallet']]", "(df['wallet']-df['phone'])"]
8